简体   繁体   中英

ListView Adapter and MainActivity

I have a list view which brings numbers from a database. I need to multiply those numbers and get total of all numbers from my listview. But the problem is that the TextView where I have to set total is in the main activity and not on the adapter. How I can send the total from an adapter to the main activity? Have I to use a loader ?

I have this in ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter{

public ArrayList<HashMap<String, String>> list;

Activity activity;
int contador = 0;
public ListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
    super();
    this.activity = activity;
    this.list = list;

}
@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}
private class ViewHolder {
TextView name;
TextView marc, cant, prec;}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();
    if (convertView == null){
        convertView = inflater.inflate(R.layout.list_colum, null);
        holder = new ViewHolder();

        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.marc = (TextView) convertView.findViewById(R.id.marc);
        holder.cant = (TextView) convertView.findViewById(R.id.cant);
        holder.prec = (TextView) convertView.findViewById(R.id.prec);
        convertView.setTag(holder);
    }
    else{

        holder=(ViewHolder) convertView.getTag();

    }

    HashMap<String, String> map = list.get(position);
    holder.name.setText(map.get(FIRST_COLUMN));
    holder.marc.setText(map.get(SECOND_COLUMN));
    holder.prec.setText(map.get(THIRD_COLUMN));
    holder.cant.setText(map.get(FOURTH_COLUMN));



    return convertView;

}

And this in MainActivity

public class MAinActivity extends AppCompatActivity {
private ArrayList<HashMap<String, String>> list;
HashMap<String, String> temp = new HashMap<String, String>();



private Button scanBtn;
private TextView total;

ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    list = new ArrayList<HashMap<String, String>>();


    setContentView(R.layout.MainActivity);

    total = (TextView)findViewById(R.id.total); //this is the TextView where I have to put the result coming from the adapter.



}


public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    listView = (ListView)findViewById(R.id.listacompras);


    ListViewAdapter adapter = new ListViewAdapter(this,list);
    listView.setAdapter(adapter);



}

you can achieve it in this way.

Create a method in your main activity like this

public updateTextView(String text){
     totla.setText(text);
}

and from adapter you can set text like this

((MAinActivity ) activity).updateTextView("New text From Adapter");

Or, you can add an interface in your ListViewAdapter

public class ListViewAdapter ...{
    public interface Listener{
        public void updateTotal(int total);
    }
    // also add a setter method for mListener
    Listener mListener;


    ....
    // call mListener.updateTotal() to show updated total
}

Then implement the listener interface in your MainActivity

public class MainActivity extends AppCompatActivity implements ListViewAdapter.Listener {
    ...

    protected void onCreate(Bundle savedInstanceState) {
        ...
        listViewAdapter.setListener(this);
    }

    public void updateTotal(int total){
       // set total here
    }
}

This is the skeleton of the code. You will need to adapt it to your code in order for it to work.

Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM