简体   繁体   中英

How can I setText to a TextView in every single row of my ListView

Hi guys I am new to Android and also to stackoverflow, Greetings and thanks for the help.

I have 2 Xml files for the same activity. One that contains a ListView and the second as my custom layout for an ArrayAdapter that populates a ListView which is also included on my first layout:

<include android:id="@+id/lv" layout="@layout/lv_row" />

ListView Custom Layout (lv_row.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/icon"
        android:src="@drawable/ic_launcher"/>
    <TextView
        android:layout_toRightOf="@+id/icon"
        android:layout_width="fill_parent"
        android:layout_height="38dp"
        android:textSize="30sp"
        android:id="@+id/label"/>
    <TextView
        android:layout_toRightOf="@+id/icon"
        android:layout_below="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="12dp"
        android:gravity="bottom"
        android:id="@+id/label2"

        android:textSize="12sp"/>

</RelativeLayout>

This is my adapter:

final ArrayAdapter<String> Array = new ArrayAdapter<String>(this,R.layout.lv_row,R.id.label,myList);

I save strings one by one into "myList" and display them on my ListView, what I want to do is to enumerate the elements on my list view by using idNum.setText(iterating_variable.toString()) ... but it won't work because idNum always get the value that was set on the XML file.

This is the code I use to link the second XML with my Java code

lv = (RelativeLayout) findViewById(R.id.lv);        
    idNum = (TextView) lv.findViewById(R.id.label2);

Thanks again for your help.

create custom Adapter and setText() in getView() method of adapter like

class CustomAdapter extends BaseAdapter {

    private ArrayList<String>   mList;
    private LayoutInflater      mInflater;
    public CustomAdapter(ArrayList<String> list) {
        mList = list;
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return mList.size() ;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) 
            convertView = mInflater.inflate(R.layout.lv_row, null);
    ((TextView) convertView.findViewById(R.id.label2)).setText(mList.get(position));
    return convertView;
    }
}

and set on ListView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM