如何将一个自定义列表的选定数据显示到同一活动的另一个自定义列表视图
Posted
技术标签:
【中文标题】如何将一个自定义列表的选定数据显示到同一活动的另一个自定义列表视图【英文标题】:how to show selected data of one custom list to another custom list view on same activity 【发布时间】:2014-11-02 10:51:45 【问题描述】:当我输入编辑文本值时,我创建了两个 textview 和一个 editText,然后单击按钮以获取第二个值中的 textview 和 edittext 值,这将显示在第二个列表视图中,但它将显示来自第一个的所有数据列表显示。
ArrayList<HashMap<String, String>> MyArrList;
String rate;
String itemn;
String quan;
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);
/*** 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));
// Get Item Input
lisView1.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// TODO Auto-generated method stub
Toast.makeText(Mmnue.this,""+ position ,Toast.LENGTH_LONG).show();
);
Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new OnClickListener()
public void onClick(View v)
TextView txtCode = (TextView)findViewById(R.id.ColCode);
TextView itemnm = (TextView)findViewById(R.id.ColID);
EditText txtInput = (EditText)findViewById(R.id.txtInput);
rate = txtCode.getText().toString();
itemnam = txtInput.getText().toString();
quan= itemnm.getText().toString();
int count = lisView1.getAdapter().getCount();
Toast.makeText(Mmnue.this, itemnam + ", " + rate+ " , " + quan ,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 CountryAdapter(OnClickListener onClickListener)
// TODO Auto-generated constructor stub
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.ColID);
txtID.setText(MyArrList.get(position).get("ID") +".");
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
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.ColID);
//txtID.setText(MyArrList.get(position).get("ID") +".");
txtID.setText(quan);
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
// txtCode.setText(MyArrList.get(position).get("Code"));
txtCode.setText(rate);
return convertView;
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in androidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
【问题讨论】:
【参考方案1】:但它会显示第一个列表视图中的所有数据。
因为您从getCount()
的CountryAdapter2
返回MyArrList.size();
。所以将其更改为1
,因为您在第二个ListView
中仅显示单行:
public int getCount()
// TODO Auto-generated method stub
return 1;
【讨论】:
但是如果我选择两条记录,那么它只会显示一条,我必须从第一个列表视图中显示所有选定的记录 @RaviKumawat :然后使用 int 计数并在 btnGetItem 单击上设置值,然后从第二个 listview 的getCount()
返回。如果用户选择了 2 行,则设置 count=2;从第二个列表视图的getCount()
返回它
@RaviKumawat : 请参阅 [如何从多选列表视图中获取所选项目](***.com/questions/4590856/…) 示例可以帮助您解决问题
好的,但是如何计算从第一个列表视图中获取选定的记录
OK thanx ..我还有一个问题我想在 toast 上显示选定的记录,在我的代码中它只在 Toast 上显示第一条记录以上是关于如何将一个自定义列表的选定数据显示到同一活动的另一个自定义列表视图的主要内容,如果未能解决你的问题,请参考以下文章
如何从自定义列表视图中获取选定项目并在 toast 消息中打印?
在 MySQL 中,如何将一张表的内容复制到同一个数据库中的另一张表中?
在 MySQL 中,如何将一张表的内容复制到同一个数据库中的另一张表中?
在 MySQL 中,如何将一张表的内容复制到同一个数据库中的另一张表中?