我在每个listView项目中都有一个TextView。我已经设置了每个TextView的值。但我无法获得TextView的价值。 每次我点击任何TextView,它都会获取ListView项的最后一个TextView的值。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.offer_list_item,parent, false);
holder.tv1 = (TextView) convertView.findViewById(R.id.offer_name);
holder.tv2 = (TextView) convertView.findViewById(R.id.expiry_date);
holder.tvoid = (TextView) convertView.findViewById(R.id.offer_id);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
OfferItem it = item.get(position);
holder.tv1.setText(it.getoffer_name());
final String message = holder.tv1.getText().toString();
holder.tv2.setText("Offer ends: " + it.getexpiry_date());
holder.tvoid.setText(it.getoffer_id());
String offerid = holder.tvoid.getText().toString();
System.out.println(offerid);
return convertView;
}
如何获取offer_id的价值..
String offerid = holder.tvoid.getText().toString();
答案 0 :(得分:1)
将值TextView
作为标记放置,然后点击已点击查看的点击标记值。
holder.tvoid.setTag(it.getoffer_id());
holder.tvoid.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int id = (int) arg0.getTag();
}
});
答案 1 :(得分:0)
试试这个..
holder.tvoid.setOnClickListener(new OnClickListener(){
@SuppressLint("NewApi")
public void onClick(View arg0) {
String offerid = holder.tvoid.getText().toString();
System.out.println(offerid);
}
});
或在listview中单击ClickListener
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
String val = ((TextView) v.findViewById(R.id.offer_id)).getText().toString().trim();
Log.v("ListSelected Item", "" + val);
System.out.println(val);
}
});
答案 2 :(得分:0)
要获取每个单击的TextView项的值,请使用ListView项单击侦听器中的以下代码: -
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long arg3) {
String str = ((TextView)view.findViewById(R.id.txtViewName)).getText().toString();
Toast.makeText(getActivity(), "" + str, 0).show();
}
};
答案 3 :(得分:0)
您可以在ListView的onItemClick侦听器中执行此操作。
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
if (view != null) {
EditText editText = (EditText) view.findViewById(R.id.edittext1);
if (editText!= null) {
String text = citiesView.getText().toString();
}
}
答案 4 :(得分:0)
单击ListView
的项目时,会触发onItemClick。您可以通过AdapterView.getItemAtPosition
Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
OfferItem it = (OfferItem)adapterView.getItemAtPosition(position);
}