2016-03-08 79 views
-6

我需要帮助来制作listview适配器。以下是代码请使用roomid将适配器的值名称。如何设置ListView适配器

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

RoomModel.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; 
} 
} 
+0

问题一点也不清楚,请您详细说明 –

+0

您正在询问代码,未修复**现有**代码。这个问题很快就会下降,不清楚,而且格式不正确。看看一些listView教程,他们会让你开始。 – Talha

+0

你为什么要求我们这样做呢?你有什么尝试? –

回答

1

尝试以下适配器......

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); 
1

确定由桑迪普的要求,我会给你简单的提示,但我不能给你完整的代码,您可以直接复制和粘贴。只要按照指示

创建,因为你需要这可能有一定的元素列表中的单个项目的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基准后,布局设置这样的适配器,并且这里modelListArrayList

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; 


    } 
} 

注:我评论,并使它尽可能简单,以帮助您。希望这可能有所帮助。如果出现任何问题,您可以评论。

+0

感谢哥们考虑我的请求:)愿上帝保佑你:)快乐编码:) –