我在活动中有一个列表视图和文本视图当我单击列表视图中的值时,所选项目将显示在文本视图中。 我附上了我的示例代码,请帮助我。
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;
}
答案 0 :(得分:4)
,为它定义一个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;
}
答案 1 :(得分: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
}
});
答案 2 :(得分: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]);
}
});
答案 3 :(得分:0)
试试这个
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
txt_disp.setText(fruits[arg2]);
}
});