【发布时间】:2014-09-23 12:34:35
【问题描述】:
我创建了两个自定义列表视图,第一个自定义列表视图有两个文本视图和一个编辑文本,在第二个自定义列表视图中有三个文本视图,我想要做的是当用户首先输入值时编辑文本,具有两个文本视图记录的编辑文本的值应显示在第一行的第二个自定义列表视图中,当用户在第二个编辑文本中输入值时,具有两个文本视图记录的编辑文本的值应显示在第二行第二个列表视图,在我的代码中,当我在编辑文本中输入值时,该编辑文本的值和两个文本视图记录都显示我第二个列表视图但是,当我在第一个自定义列表视图的第二个编辑文本中输入值时值也显示在第二个自定义列表视图中,但它显示在第一行,我想在第二个自定义列表视图的第二行显示该记录。
public class MainActivity extends Activity {
ArrayList<Candy> myArrList;
EditText text;
List<String> a = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myArrList = new ArrayList<Candy>();
ListView lisView1 = (ListView)findViewById(R.id.listView1);
ListView lisView2 = (ListView)findViewById(R.id.listView2);
myArrList.add(new Candy("Butterscotch", "10"));
myArrList.add(new Candy("Birthday Cake", "100"));
myArrList.add(new Candy("Black Crunch", "102"));
myArrList.add(new Candy("Industrial Chocolate", "200"));
myArrList.add(new Candy("Coffee Molasses Chip", "500"));
lisView1.setAdapter(new CountryAdapter(this));
lisView2.setAdapter(new CountryAdapter2(getApplicationContext()));
}
//This is base adapter for first list view
public class CountryAdapter extends BaseAdapter
{
private Context context;
public CountryAdapter(Context c)
{
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).getName() +".");
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
txtCode.setText(myArrList.get(position).getRate());
text = (EditText)convertView.findViewById(R.id.txtInput);
text.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s)
{
TextView txtCode = (TextView)findViewById(R.id.secnm);
TextView txtID = (TextView)findViewById(R.id.secrat);
TextView tv = (TextView)findViewById(R.id.sectxtInput);
txtCode.setText(myArrList.get(position).getName() +".");
tv.setText(s);
txtID.setText(myArrList.get(position).getRate());
Toast.makeText(getApplicationContext(), "hello"+s+""+myArrList.get(position).getName()+""+myArrList.get(position).getRate(), 50).show(); }
});
return convertView;}}
//This is second adapter for second list view
public class CountryAdapter2 extends BaseAdapter
{
private Context context;
public CountryAdapter2(Context c)
{
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) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{convertView = inflater.inflate(R.layout.secmmnue, null);
}
return convertView;
}
【问题讨论】:
-
您需要为每个视图添加一个
TextWatcher,以便在文本更改后立即在其中执行某项操作。然后如果是editview 1,则将文本添加到tv1等,如果是editview 2,则将文本添加到tv2 ... -
我用它看我的代码
-
请删除所有不相关的代码 - 只留下相关的代码部分。
-
阅读我的问题我想做什么..
-
整个代码很重要..
标签: android android-listview textwatcher