【问题标题】:how to show two text view record with edit text record of first custom list view to in second Custom list view on Edit text afterTextChanged如何在编辑文本 afterTextChanged 上的第二个自定义列表视图中显示两个文本视图记录以及第一个自定义列表视图的编辑文本记录
【发布时间】: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


【解决方案1】:

很难理解您要做什么,但有一点很清楚:您似乎不了解列表视图的工作原理。您不能直接更改列表视图的显示元素 - 您必须更改适配器后面的数据,然后调用 notifyDataSetChanged

您需要有一些数据结构来保存每个列表视图的数据。在第一个 ListView 的适配器中,您需要引用第二个 ListView 的适配器的数据结构。然后,在您的文本观察器中,当您需要更新第二个列表视图中的某些内容时,您需要为第二个列表视图修改该数据结构中的数据并在其上调用notifyDataSetChanged 以使更改反映在屏幕上。

显然,您需要修改第二个适配器(特别是 getView 方法),以根据数据结构的内容和行的索引创建视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2012-02-22
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    相关资源
    最近更新 更多