0
votes

I'm trying to complete a code that controls a custom ListView through three classes: AdapterListView, ItemListView and MainActivity.

So far, the code can only enter data and images (inserted by code) through AdapterListView.

I added in my item_list.xml two buttons, a button to remove the item from the list and another button to edit the list item.

I tried to implement the method to remove the item and insert in my AdapterListView but I did not know how to call it in MainActivity.

    public void deletaItem(View v) {
    adapterListView.removeItem(?????);
    }

I also need to know how do I edit the item after clicking on the edit button.

Below the three project classes:

AdapterListView

package com.example.aulasandroid.aulas_android_listviewactions;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by lmontanhine on 11/2/2015.
*/
public class AdapterListView extends BaseAdapter {

private LayoutInflater mInflater;
private ArrayList<ItemListView> itens;

public AdapterListView(Context context, ArrayList<ItemListView> itens) {
    //Itens que preencheram o listview
    this.itens = itens;
    //responsavel por pegar o Layout do item.
    mInflater = LayoutInflater.from(context);
}

/**
 * Retorna a quantidade de itens
 *
 * @return
 */
public int getCount() {
    return itens.size();
}

/**
 * Retorna o item de acordo com a posicao dele na tela.
 *
 * @param position
 * @return
 */
public ItemListView getItem(int position) {
    return itens.get(position);
}

/**
 * Sem implementação
 *
 * @param position
 * @return
 */
public long getItemId(int position) {
    return position;
}

public View getView(int position, View view, ViewGroup parent) {
    //Pega o item de acordo com a posção.
    ItemListView item = itens.get(position);
    //infla o layout para podermos preencher os dados
    view = mInflater.inflate(R.layout.item_list, null);

    //atraves do layout pego pelo LayoutInflater, pegamos cada id relacionado
    //ao item e definimos as informações.
    ((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
    ((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());

    return view;
}
public void removeItem(int positionToRemove){
    itens.remove(positionToRemove);
    notifyDataSetChanged();
}
}

ItemListView

package com.example.aulasandroid.aulas_android_listviewactions;

/**
* Created by lmontanhine on 11/2/2015.
*/
public class ItemListView {

private String texto;
private int iconeRid;

public ItemListView() {
}

public ItemListView(String texto, int iconeRid) {
    this.texto = texto;
    this.iconeRid = iconeRid;
}

public int getIconeRid() {
    return iconeRid;
}

public void setIconeRid(int iconeRid) {
    this.iconeRid = iconeRid;
}

public String getTexto() {
    return texto;
}

public void setTexto(String texto) {
    this.texto = texto;
}
}

MainActivity

package com.example.aulasandroid.aulas_android_listviewactions;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;


public class MainActivity extends Activity implements    AdapterView.OnItemClickListener {

private ListView listView;
private AdapterListView adapterListView;
private ArrayList<ItemListView> itens;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //carrega o layout onde contem o ListView
    setContentView(R.layout.activity_main);

    //Pega a referencia do ListView
    listView = (ListView) findViewById(R.id.list);
    //Define o Listener quando alguem clicar no item.
    listView.setOnItemClickListener(this);

    createListView();
}

private void createListView() {
    //Criamos nossa lista que preenchera o ListView
    itens = new ArrayList<ItemListView>();
    ItemListView item1 = new ItemListView("Felpudo", R.drawable.felpudo);
    ItemListView item2 = new ItemListView("Felpudão", R.drawable.felpudo1);
    ItemListView item3 = new ItemListView("Felpudinho", R.drawable.felpudo2);

    itens.add(item1);
    itens.add(item2);
    itens.add(item3);

    //Cria o adapter
    adapterListView = new AdapterListView(this, itens);

    //Define o Adapter
    listView.setAdapter(adapterListView);
    //Cor quando a lista é selecionada para ralagem.
    listView.setCacheColorHint(Color.TRANSPARENT);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //Pega o item que foi selecionado.
    ItemListView item = adapterListView.getItem(arg2);
    //Demostração
    Toast.makeText(this, "Você Clicou em: " + item.getTexto(), Toast.LENGTH_LONG).show();
}

public void deletaItem(View v) {
    adapterListView.removeItem();
}
}

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5sp">
    <ImageView
        android:id="@+id/imagemview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />
    <Button
        android:id="@+id/btnEdit"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Editar"
        android:textColor="#FFFFFF"/>

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnEdit"
        android:layout_marginTop="3dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Deletar"
        android:textColor="#FFFFFF"
        android:onClick="deletaItem"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_marginLeft="115sp"
        android:gravity="center_vertical"
        android:textColor="#FF000000"
        />

</RelativeLayout>
</RelativeLayout>
1

1 Answers

1
votes

in the getView method, you can set a tag to each delete button, something like:

((Button) view.findViewById(R.id.btnDelete)).setTag(position); 

(position is the first param of getView inside the adapter).

then, in the deletaItem method, you can ask for v.getTag(), and this value will be the param for removeItem.

public void deletaItem(View v) {
    adapterListView.removeItem(v.getTag());
}

EDIT: is pretty much the same.

inside adapter's getView:

((Button) view.findViewById(R.id.btnEdit)).setTag(position); 

inside adapter class, create a new method, something like:

public void editItem(int position) {
    // edit behavior
    .
    .
    .
    notifyDataSetChanged();
}

inside activity:

public void onClickEditButton(View v) {
    adapterListView.editItem(v.getTag());
}

also, don't forget to add onClick="onClickEditButton" on item_list.xml

hope it helps :D

EDIT 2: to edit the item text

 public void editItem(int position) {
    getItem(position).setTexto("new text");
    notifyDataSetChanged();
 }