gpt4 book ai didi

java - 我想将一个自定义 ListView 记录显示到第二个自定义 ListView

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:37 25 4
gpt4 key购买 nike

我创建了两个自定义 ListView ,都有两个 TextView 和一个 EditText ,我想显示输入 edittext 的所有记录,在按钮单击事件上接受第二个自定义 ListView 上的 TextView ,在我的代码中它不会向任何人显示第二个 ListView 中的东西但它显示在 toast 上

ArrayList<HashMap<String, String>> MyArrList;
String rate ;
String itemnam ;
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);


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 nam = (TextView)itemLayout.findViewById(R.id.ColID);

TextView rat = (TextView)itemLayout.findViewById(R.id.ColCode);

EditText quan = (EditText)itemLayout.findViewById(R.id.txtInput);

String Code = rat.getText().toString();
String Country = quan.getText().toString();
String name = nam.getText().toString();

Log.d(Code, Country);
Toast.makeText(Mmnue.this,name + ", " + Country+ " , " +Code ,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.ColID);

txtID.setText(MyArrList.get(position).get("ID") +".");

// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
txtCode.setText(MyArrList.get(position).get("Code"));

// ColCode
//EditText quan = (EditText) convertView.findViewById(R.id.txtInput);
// quan.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 1;
}

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

EditText quant = (EditText) convertView.findViewById(R.id.txtInput);
// txtCode.setText(MyArrList.get(position).get("Code"));
quant.setText(itemnam);

return convertView;

}

最佳答案

String rate ;
String itemnam ;
String quan;

你在哪里初始化它们?

首先您按照以下方式进行操作,但现在您创建了本地变量:

String Code = rat.getText().toString();
String Country = quan.getText().toString();
String name = nam.getText().toString();

在设置适配器之前您无需执行任何操作

lisView2.setAdapter(new CountryAdapter2(getApplicationContext()));

什么都不会发生:

 txtID.setText(quan);
txtCode.setText(rate);
quant.setText(itemnam);

您应该填充一个 arrayList 并在第二个适配器中使用第二个 arraylist。就像你的第一次一样。

关于java - 我想将一个自定义 ListView 记录显示到第二个自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25743342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com