我在 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;
}