Android listview - Вземете текст на персонализиран списък

Разработвам приложение, в което създадох персонализиран изглед на списък като:

Изгледът на списъка xml код като:

<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 файлът на адаптера е:

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

След това задавам слушател за щракване върху елемент в изглед на списък като:

listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);    

ListAdapter adapter = new ListAdapter(this,list1,list2);
listView.setAdapter(adapter);

След това използвах onItemClickListener при щракване върху елемент като:

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
name = (String) arg0.getItemAtPosition(position);       
}

Тук получавам стойността на първия текстов изглед на персонализиран списък. Но искам и стойността на втория текстов изглед. и ако добавя повече, тогава как да получа стойности от това.

Моля, предложете ми начина за това.


person Manoj Fegde    schedule 20.03.2013    source източник
comment
ако някои от отговорите са помогнали, моля, приемете ги, така че този въпрос да не се показва като без отговор.   -  person Marko Niciforovic    schedule 20.03.2013
comment
вижте множество начини за получаване на текст на listView stackoverflow.com/a/28479593/3496570   -  person AndroidGeek    schedule 12.02.2015


Отговори (3)


Опитайте по този начин вътре в OnItemClick

TextView text = (TextView) arg1.findViewById(R.id.urtextID);
                           ^^^^^^
String tEXT = text.getText().toString();
person nidhi_adiga    schedule 20.03.2013
comment
това винаги показва текста на първия елемент, а не този в позицията, върху която е щракнал - person Raa; 12.01.2014
comment
arg1, т.е. променливата View е важна, поставихте ли това правилно във вашата реализация...? - person nidhi_adiga; 12.01.2014

просто опростена версия на горния отговор

    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // code in the commented section works in case of normal single textView

    // TextView temp=(TextView) view; 
    // Toast.makeText(this,
    // temp.getText()+" is pressed "+position,Toast.LENGTH_SHORT
    // ).show();

    TextView temp = (TextView) view.findViewById(R.id.textView1);

    String str = temp.getText().toString();
    Toast.makeText(this,
            str + " is pressed " + position,
            Toast.LENGTH_SHORT).show();
}
person ahmed    schedule 12.08.2014

Опитайте тази,

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
TextView textView = (TextView) arg1.findViewById(R.id.urtextID);
String text = textView.getText().toString();   
Log.d("TEXT", "Text is" + text);   
}

горните отговори са същите като моя отговор.....

person sourab maity    schedule 28.10.2020