简体   繁体   English

将第二个textview添加到customAdapter ListView

[英]Adding a second textview to customAdapter ListView

So I have a listview with one textview and I want to add a second textview to it. 所以我有一个带有一个textview的listview,我想向它添加第二个textview。 But I cannot figure out how to modify the adapter and then call it in my main activity, even after looking through many similiar questions on stackoverflow. 但是,即使浏览了许多关于stackoverflow的类似问题,我也无法弄清楚如何修改适配器然后在我的主要活动中调用它。

Heres how my CustomAdapter.java is now 这是我的CustomAdapter.java现在的样子

 class CustomAdapter extends ArrayAdapter<CharSequence>{

    public CustomAdapter(Context context, CharSequence[] routes) {
        super(context, R.layout.custom_row ,routes);
    }

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater routeInflater = LayoutInflater.from(getContext());
    View customView = convertView;
    if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}

    CharSequence singleRoute = getItem(position);
    TextView routeText = (TextView) customView.findViewById(R.id.routeText);
    routeText.setText(singleRoute);

    ///// Textview I want to add
    CharSequence routeNum = getItem(position);
    TextView routeNumText = (TextView) customView.findViewById(R.id.numbersTextView);
    routeNumText.setText(routeNum);
    /////
    return customView;

and heres my MainActivity.java 这是我的MainActivity.java

 ///// fill listview numbers I want to add
    final String[] routeListviewNumbers = getResources().getStringArray(R.array.routeNumbers);
    //fill list view with xml array of routes
    final String[] routeListViewItems = getResources().getStringArray(R.array.routeList);

   //custom adapter for list view
    ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
    final ListView routeListView = (ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(routeAdapter);

any help in how to modify the adapter and then call it in the main activity would be really appreciated. 非常感谢您对如何修改适配器然后在主要活动中调用它的任何帮助。 Thank you! 谢谢!

I would recommend creating a custom Route class since this adapter is meant to handle one array. 我建议创建一个自定义Route类,因为此适配器旨在处理一个数组。 Create some member variables for the route number and route items in the new class with getter and setter methods. 使用getter和setter方法在新类中为路由号和路由项创建一些成员变量。 Then you should be able to create a new array list of Route objects in your main activity and iterate through the existing string arrays while appending them (as new Route objects) to the new array. 然后,您应该能够在主要活动中创建Route对象的新数组列表,并在将现有字符串数组(作为新Route对象)追加到新数组的同时迭代现有字符串数组。

You will have to change the adapter to accept a Route object instead of CharSequence. 您将必须更改适配器以接受Route对象而不是CharSequence。 Hope this points you in the right direction. 希望这能为您指明正确的方向。

I would recommend a custom adapter for you. 我会为您推荐一个自定义适配器。 Exm.. Create Route Class Exm ..创建路线类别

class Route{
     public int number;
     public String text;
}

Base Adapter... 底座适配器...

public class myBaseAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Route> mRouteList;

public BilgiAdapter(Activity activity,List<Route> routeList){
    mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mRouteList = routeList;
}

@Override
public int getCount() {
    return mRouteList.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View myView;
    myView = mInflater.inflate(R.layout.line_layout,null);
    Route r = mRouteList.get(position);

    TextView txtRouteNumber = (TextView)myView.findViewById(R.id.textRouteNumber);
    TextView txtRouteText = (TextView)myView.findViewById(R.id.textRouteText);

    txtRouteNumber.setText(String.ValueOf(r.number));
    txtRouteText.setText(String.ValueOf(r.text));
    return myView;
}

} }

MainActivity vs.. MainActivity vs.

ListView lstRoute;
myBaseAdapter adapter;
List<Route> list;
...
..
..
..
..
OnCreate(..)
..
list = new ArrayList<Route>();
//add routes in list
myBaseAdapter = new myBaseAdapter(MainActivity.this,list);
lstRoute = (ListView)findViewById(R.id.listViewRoute);
lstRoute.setAdapter(myBaseAdapter);
...
..
..

line_layout.xml(Layout file) line_layout.xml(布局文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textRouteNumber"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:id="@+id/textRouteText"/>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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