当我输入编辑文本值然后单击按钮以获取第二个值中的textview和edittext值时,我创建了两个textview和一个editText,这将显示在第二个listview中,但它将显示来自第一个的所有数据列表显示。
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 +&#34;,&#34; + rate +&#34;,&#34; + 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);
}
}
答案 0 :(得分:0)
但它会显示第一个列表视图中的所有数据。
因为您从MyArrList.size();
的{{1}}返回getCount()
。因此,将其更改为CountryAdapter2
,因为您只在第二个1
中显示单行:
ListView