有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何设置ListView适配器

我需要帮助来制作listview适配器。下面是代码,请将适配器的值名称与roomid一起设置

JSONArray rooms = jsonObject.getJSONArray("rooms");
     for (int i = 0; i < rooms.length(); i++) {
        JSONObject room = rooms.getJSONObject(i);
       String name = room.optString("room");
       String roomid = room.optString("roomid");
      final RoomModel sched = new RoomModel();
       sched.setName(name);
       sched.setroomId(roomid);
    CustomListViewValuesArr.add(sched);}        
listView = (ListView) findViewById(R.id.ChatlistView);

房间模型。java

public class RoomModel {
    private  String name, id,  roomid;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}


public String getroomId() {
    return roomid;
}

public void setroomId(String roomid) {
    this.roomid = roomid;
}
}

共 (2) 个答案

  1. # 1 楼答案

    好的,按照Sandeep的要求,我会给你们简单的提示,但我不能给你们完整的代码,你们可以直接复制和粘贴。按照指示去做就行了

    为列表中的单个项目创建一个XML文件,该项目可能包含您需要的某些元素,例如它的single_item.xml有3 TextView

    <?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:id="@+id/tvId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Sample Id"
            android:textColor="#0414f4"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="10sp" />
    
        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:text="Sample Title"
            android:textColor="#000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20dp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/tvBody"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sample body"
            android:textSize="16sp"/>
    </LinearLayout>
    

    在从主布局获得listView的引用之后,像这样设置适配器,这里modelList是一个ArrayList

        mAdapter = new CustomAdapter(MainActivity.this, R.layout.single_item, modelList);
        mAdapter.notifyDataSetChanged();
        mListView.setAdapter(mAdapter);
    

    然后类CustomAdapter如下所示

    public class CustomAdapter extends ArrayAdapter<Model> {    
        ArrayList<Model> modelList;
        Context context;    
    
        public CustomAdapter(Context context, int resource, ArrayList<Model> objects) {
            super(context, resource, objects);
    
            this.modelList = objects;
            this.context = context;
    
        }   
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
    
    
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
    
                convertView = LayoutInflater.from(context).inflate(R.layout.single_item,parent,false);
    
            }
    
            // Lookup view for data population
            TextView tvId = (TextView) convertView.findViewById(R.id.tvId);
            TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            TextView tvBody = (TextView) convertView.findViewById(R.id.tvBody);
    
    
    
            // Populate the data into the template view using the data object
            tvId.setText(String.valueOf(model.getId()));
            tvTitle.setText(model.getroomId());
            tvBody.setText(model.getName());
    
            // Return the completed view to render on screen
            return convertView;
    
    
        }
    }
    

    注意:我已经发表了评论,并尽可能简单地帮助您。希望这能有所帮助。如果出现任何问题,您可以发表评论

  2. # 2 楼答案

    请尝试以下适配器

        public class MyAdapter extends BaseAdapter {
    
            Context con;
            ArrayList<your type> mlist;
            RoomModel sched;
    
            public MyAdapter(Context con,ArrayList<your type> mlist )
            {
                this.con=con;
                this.mlist=mlist;
    
            }
    
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mlist.length;
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return mlist[position];
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
    
          sched=mlist.get(position);
            LayoutInflater inflater=(LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.your_layout,parent,false);
    
    
            TextView tv1=(TextView)convertView.findViewById(R.id.your_textview);
            tv1.setText(sched.getId());
    
           TextView tv2=(TextView)convertView.findViewById(R.id.your_textview);
            tv2.setText(sched.getName());
    
           TextView tv3=(TextView)convertView.findViewById(R.id.your_textview);
            tv3.setText(sched.getroomId());
    
                return convertView;
            }
    
      }
    

    并更改以下代码

    JSONArray rooms = jsonObject.getJSONArray("rooms");
         for (int i = 0; i < rooms.length(); i++) {
            JSONObject room = rooms.getJSONObject(i);
           String name = room.optString("room");
           String roomid = room.optString("roomid");
          final RoomModel sched = new RoomModel();
           sched.setName(name);
           sched.setroomId(roomid);
        CustomListViewValuesArr.add(sched);}        
    listView = (ListView) findViewById(R.id.ChatlistView);
    MyAdapter adapter=new MyAdapter(this,CustomListViewValuesArr);
    listView.setAdapter(adapter);