CustomAdapter中的2个textView

时间:2019-05-30 22:53:55

标签: java listview arraylist android-listview textview

我有一些代码在textView1中显示元素数组。使用来自textView1的CustomAdapter收集listView。我需要添加textView2。

listviewcustom.xml

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, ArrayList<String> records){
        super (context, 0, records);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        String item = getItem(position);

        if (convertView==null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
        }

        final TextView textView1 = (TextView)convertView.findViewById(R.id.textView1);
        final TextView textView2 = (TextView)convertView.findViewById(R.id.textView2);        

        textView1.setText(item);
        textView2.setText(item);      

        return  convertView;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        

        ListView list = (ListView) findViewById(R.id.listView);

        ArrayList<String> title = new ArrayList<String>();
        title.add("title1");
        title.add("title2");
        title.add("title3");
        title.add("title4");
        title.add("title5");
        title.add("title6");

        ArrayList<String> myData = new ArrayList<String>();
        myData.add("1");
        myData.add("2");
        myData.add("3");
        myData.add("4");
        myData.add("5");
        myData.add("6");       

        MyAdapter adapter = new MyAdapter(this, myData);

        list.setAdapter(adapter);

    }
}

我需要在listView中将值从标题转移到textViev1,将myData转移到textView2

0 个答案:

没有答案