本文介绍了Android的列表视图 - 获取自定义列表视图的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发中,我创建了一个自定义列表视图的应用程序:
I am developing an application in which i created a custom list view as:
列表视图XML code为:
The list view xml code as :
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="15dip"
android:divider="#623b42"
android:dividerHeight="0.5dip"
android:cacheColorHint="#00000000"
android:overScrollMode="never" >
</ListView>
和适配器XML是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textview_rupee_mlsaa"
android:gravity="center_vertical"
android:padding="15dip"
android:textColor="@color/color_black"
android:textSize="15sp"
tools:ignore="SelectableText" />
<TextView
android:id="@+id/textview2"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:padding="15dip"
android:textColor="@color/color_black"
android:textSize="15sp"
tools:ignore="SelectableText" />
</RelativeLayout>
适配器的java文件是:
The .java file of adapter is :
public class ListAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] dataset1;
private int[] dataset2;
/** Constructor Class */
public ListAdapter(Context context,String[] ListArray1, int[] ListArray2) {
super(context,R.layout.list_adapter_layout,ListArray);
this.context = context;
this.dataset1= ListArray1;
this.dataset2= ListArray2;
}
/** Implement getView method for customizing row of list view. */
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
// Creating a view of row.
View rowView = inflater.inflate(R.layout.list_adapter_layout, parent, false);
TextView tv1 = (TextView)rowView.findViewById(R.id.textview1);
TextView tv2 = (TextView)rowView.findViewById(R.id.textview2);
tv1.setText(data1[position]);
tv2.setText(""+data2[position]);
return rowView;
}
}
然后我设置项目点击监听器列表查看方式:
Then i set on item click listener on list view as :
listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);
ListAdapter adapter = new ListAdapter(this,list1,list2);
listView.setAdapter(adapter);
然后我用onItemClickListener项目上点击如下:
Then i used onItemClickListener on item click as :
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
name = (String) arg0.getItemAtPosition(position);
}
我在这里获取自定义列表视图的第一个文本浏览的价值。但我也想第二个文本浏览的价值。如果我增加更多的那么如何得到该值。
Here i am getting the value of first text view of custom listview. But i want also the value of second text view. and if i add more then how to get values of that.
请建议我这样做的方式。
Please suggest me the way to do that.
推荐答案
试着像这里面乌尔OnItemClick
Try like this inside ur OnItemClick
TextView text = (TextView) arg1.findViewById(R.id.urtextID);
^^^^^^
String tEXT = text.getText().toString();
这篇关于Android的列表视图 - 获取自定义列表视图的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!