我创建了两个自定义listView,都有两个textView和一个EditText,我想显示在edittext中输入的所有记录在第二个自定义ListView上接受textView但是当我点击按钮获取记录时,它只显示最后一个记录在第二个自定义listView!
public class Mmnue extends Activity {
ArrayList<HashMap<String, String>> MyArrList;
String rate ;
String name ;
String quant;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuitem);
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
final ListView lisView2 = (ListView)findViewById(R.id.listView2);
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
/*** Rows 1 ***/
map = new HashMap<String, String>();
map.put("ID", "Butterscotch");
map.put("Code", "Rs 10");
MyArrList.add(map);
/*** Rows 2 ***/
map = new HashMap<String, String>();
map.put("ID", "Birthday Cake");
map.put("Code", "Rs 100");
MyArrList.add(map);
/*** Rows 3 ***/
map = new HashMap<String, String>();
map.put("ID", "Black Crunch");
map.put("Code", "Rs 102");
MyArrList.add(map);
/*** Rows 4 ***/
map = new HashMap<String, String>();
map.put("ID", "Industrial Chocolate");
map.put("Code", "Rs 200");
MyArrList.add(map);
/*** Rows 5 ***/
map = new HashMap<String, String>();
map.put("ID", "Coffee Molasses Chip");
map.put("Code", " Rs 500");
MyArrList.add(map);
lisView1.setAdapter(new CountryAdapter(this));
Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int count = lisView1.getAdapter().getCount();
for (int i = 0; i < count; i++) {
LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
TextView itemname = (TextView)itemLayout.findViewById(R.id.nm);
TextView rat = (TextView)itemLayout.findViewById(R.id.rat);
EditText quan = (EditText)itemLayout.findViewById(R.id.txtInput);
rate = rat.getText().toString();
quant = quan.getText().toString();
name = itemname.getText().toString();
Toast.makeText(Mmnue.this,name + ", " + rate+ " , " +quant ,Toast.LENGTH_LONG).show();
lisView2.setAdapter(new CountryAdapter2(getApplicationContext()));
}
}
});
}
public class CountryAdapter extends BaseAdapter
{
private Context context;
public CountryAdapter(Context c)
{
//super( c, R.layout.activity_column, R.id.rowTextView, );
// TODO Auto-generated method stub
context = c;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_mmnue, null);
}
// ColID
TextView txtID = (TextView) convertView.findViewById(R.id.nm);
txtID.setText(MyArrList.get(position).get("ID") +".");
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
txtCode.setText(MyArrList.get(position).get("Code"));
return convertView;
}
}
public class CountryAdapter2 extends BaseAdapter
{
private Context context;
public CountryAdapter2(Context c)
{
//super( c, R.layout.activity_column, R.id.rowTextView, );
// TODO Auto-generated method stub
context = c;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_mmnue, null);
}
// ColID
TextView txtID = (TextView) convertView.findViewById(R.id.nm);
//txtID.setText(MyArrList.get(position).get("ID") +".");
txtID.setText(name);
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
// txtCode.setText(MyArrList.get(position).get("Code"));
txtCode.setText(rate);
EditText quan = (EditText) convertView.findViewById(R.id.txtInput);
// txtCode.setText(MyArrList.get(position).get("Code"));
quan.setText(quant);
Toast.makeText(getApplicationContext(),""+quant+""+rate+""+name, Toast.LENGTH_LONG).show();
return convertView;
}
}
答案 0 :(得分:0)
我不确定您究竟在问什么,但似乎您希望将ListView1中公开的相同数据复制到ListView2。由于ListViews由Adapters支持,而你的是CountryAdapter,因此其中有一个ArrayList。您可以添加一个名为public ArrayList getData()
的方法,该方法返回您的ArrayList,以便您可以填充ListView2。
答案 1 :(得分:0)
在for循环中,您将名称,quant和rate存储在每次循环时都会被覆盖的变量中。要解决这个问题,您可以考虑将所有值存储在数组中,然后一旦拥有整个数据数组,就可以更新第二个listView。
您的问题点
for (int i = 0; i < count; i++) {
LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
TextView itemname = (TextView)itemLayout.findViewById(R.id.nm);
TextView rat = (TextView)itemLayout.findViewById(R.id.rat);
EditText quan = (EditText)itemLayout.findViewById(R.id.txtInput);
rate = rat.getText().toString();
quant = quan.getText().toString();
name = itemname.getText().toString();
一个解决方案
String rate[count], quant[count], name[count];
for (int i = 0; i < count; i++) {
LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
TextView itemname = (TextView)itemLayout.findViewById(R.id.nm);
TextView rat = (TextView)itemLayout.findViewById(R.id.rat);
EditText quan = (EditText)itemLayout.findViewById(R.id.txtInput);
rate[i] = rat.getText().toString();
quant[i] = quan.getText().toString();
name[i] = itemname.getText().toString();
在按钮上单击,更改数据集并使用adapter.notifyDataSetChanged()
更新listView
Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Change ListView Data Set
adapter.notifyDataSetChanged();
}
});