ListView适配器和MainActivity

时间:2016-04-01 05:30:25

标签: java android

我有一个列表视图,它带来了数据库中的数字。我需要将这些数字相乘,并从我的列表视图中获取所有数字的总和。但问题是我必须设置总计的TextView是在主活动而不是在适配器上。如何将总数从适配器发送到主要活动? 我有没有使用装载机?

我在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;

}

这在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);



}

2 个答案:

答案 0 :(得分:0)

你可以通过这种方式实现它。

在您的主要活动中创建一个方法

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

从适配器您可以像这样设置文本

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

答案 1 :(得分:0)

或者,您可以在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
}

然后在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
    }
}

这是代码的骨架。您需要根据代码对其进行调整才能使其正常工作。

祝你好运:)