2

我在 Activty 中有一个 listview 和 textview 当我单击 listview 中的一个值时,该选定项目将显示在一个 textivew 中。我附上了我的示例代码,请帮助我。

在此处输入图像描述

MainActivity.java
public class MainActivity extends Activity {
TextView txt_disp;
ListView lv;
String fruits[]={"Apple","Orange","Banana","Grapes","kiwi"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt_disp=(TextView)findViewById(R.id.textView1);
    lv=(ListView)findViewById(R.id.listView1);
     ArrayList<SamplePOJO> searchResults = GetSearchResults();
        CustomAdapter ea=new CustomAdapter(this, searchResults);
             lv.setAdapter(ea);
    // here i need to set the text(txt_display)  
}  }

CustomAdapter.java
 public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
         convertView = mInflater.inflate(R.layout.row1, null);
        holder = new ViewHolder();
        holder.fruit=(TextView) convertView.findViewById(R.id.txt_fruit);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.fruit.setText(ContactArrayList.get(position).getFruit());
    holder.fruit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
            // TODO Auto-generated method stub
            String dis=holder.fruit.getText().toString();
            System.out.println("Selected Text:"+dis);
                 //Selected text value  set into textview in MainActivity 
    }
    });
    return convertView;
  }
4

4 回答 4

4

在您的 CustomAdapter 中,为它定义一个 TextView 字段和一个 setter,然后在 onCreate 中将 textField 设置为您使用 findviewbyid 拉出的文本字段

MainActivity.java
public class MainActivity extends Activity {
TextView txt_disp;
ListView lv;
String fruits[]={"Apple","Orange","Banana","Grapes","kiwi"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt_disp=(TextView)findViewById(R.id.textView1);
    lv=(ListView)findViewById(R.id.listView1);
     ArrayList<SamplePOJO> searchResults = GetSearchResults();
        CustomAdapter ea=new CustomAdapter(this, searchResults);
             lv.setAdapter(ea);
    // here i need to set the text(txt_display)
ea.setTextField(txt_disp);


}  }

CustomAdapter.java

TextView textView;

public void setTextField(TextView tv){textView = tv;}
enter code here
 public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
         convertView = mInflater.inflate(R.layout.row1, null);
        holder = new ViewHolder();
        holder.fruit=(TextView) convertView.findViewById(R.id.txt_fruit);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.fruit.setText(ContactArrayList.get(position).getFruit());
    holder.fruit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
            // TODO Auto-generated method stub
            String dis=holder.fruit.getText().toString();
            System.out.println("Selected Text:"+dis);
                 //Selected text value  set into textview in MainActivity
            textView.setText("Selected Text:"+dis);
    }
    });
    return convertView;
  }
于 2013-09-27T06:38:06.573 回答
1

我认为你必须听列表的项目点击事件你可以通过添加这个代码段来做到这一点

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            // set the value to the text view here
        }
    });
于 2013-09-27T06:39:26.623 回答
0

尝试这个

lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                txt_disp.setText(fruits[arg2]);
            }
        });
于 2013-09-27T06:47:15.653 回答
0

尝试:

@Override
 public void onClick(View v) {
    // TODO Auto-generated method stub
    String dis=holder.fruit.getText().toString();
    System.out.println("Selected Text:"+dis);

    MainActivity.this.txt_disp.setText(dis);
 }
});

编辑 :

我没有注意到这些是单独的课程。尝试在 Activity 中设置 ListViev 单击侦听器您将获得所选项目和项目数组的位置。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        txt_disp.setText(fruits[position]);
    }
});
于 2013-09-27T06:35:33.260 回答