2016-04-01 65 views
-2

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

我有这样的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); 



} 
+0

使用broadcastReceiver。 –

回答

0

你可以用这种方式实现它。

在这样

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

主要活动创建方法和适配器,你可以设置文字这样

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

我的天啊!太容易了!谢谢Mustanser lqbal! –

+0

你可以接受答案。如果这回答你的问题。 –

+0

是的。当然!谢谢! –

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 
    } 
} 

这是代码的骨架。您需要将其调整为适合您的代码才能运行。

祝你好运:)

+0

天啊!太简单了!非常感谢! –