我试图在我的Android应用程序中显示roomid,但它们不是节目,因为我是初学者。请写下代码。我已经在下面给你看了我的代码,
RoomListActivity.java短代码
ArrayList<String> items = new ArrayList<String>();
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");
System.out.println("OutPut Name:"+ name.toString());
System.out.println("OutPut roomid:"+ roomid.toString());
items.add(name);
}
ListView listView = (ListView) findViewById(R.id.ChatlistView);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(RoomListActivity.this,
R.layout.room_row,
R.id.room,
items);
listView.setAdapter(mArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView = (TextView)view.findViewById(R.id.room);
String message = "You clicked # " + position + ", which is string: " + textView.getText().toString();
Toast.makeText(RoomListActivity.this, message, Toast.LENGTH_LONG).show();
}
});
room_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/img_room"
android:layout_toRightOf="@+id/img_room"
android:layout_toStartOf="@+id/img_room"
android:gravity="left|center_vertical"
android:text="Room Name"
android:textSize="16dp" />
<ImageView
android:id="@+id/img_room"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="@drawable/chat" />
<TextView
android:id="@+id/roomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/room"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/img_room"
android:layout_toStartOf="@+id/img_room"
android:layout_below="@+id/room"
android:gravity="left|center_vertical"
android:text="Room Id" />
</RelativeLayout>
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;}
}
OutPut名称:OutPut会议室:记录
03-10 13:35:33.315: I/System.out(726): OutPut Name:.~=cAsiNo-RooM=~.
03-10 13:35:33.325: I/System.out(726): OutPut roomid:room12
03-10 13:35:33.325: I/System.out(726): OutPut Name:.~=MusiC-Masti=~.
03-10 13:35:33.335: I/System.out(726): OutPut roomid:room24
答案 0 :(得分:0)
尝试使用此适配器:
public class RoomAdapter extends ArrayAdapter<RoomModel> {
private final String TAG = RoomAdapter.class.getSimpleName();
private final Activity context;
private List<RoomModel> values;
public RoomAdapter(Activity context, List<RoomModel> values) {
super(context, android.R.layout.room_row.xml, values);
this.context = context;
this.values = values;
}
public int getCount() {
return values.size();
}
public String getItem(int position) {
return values.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(android.R.layout.room_row.xml, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
viewHolder.name = (TextView) rowView.findViewById(R.id.room);
viewHolder.image = (ImageView) rowView.findViewById(R.id.img_room);
}
viewHolder.name.setText(getItem(position).getName());
// dependencies { compile 'com.github.bumptech.glide:glide:3.7.0' }
Glide.with(context)
.load( "URL_PICTURE"))
.placeholder(R.drawable.chat)
.error(R.drawable.chat)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewHolder.image);
return convertView;
}
static class ViewHolder {
TextView name ;
ImageView image;
}
}
改变:
ArrayList<String> items = new ArrayList<String>();
致:
ArrayList<RoomModel> items = new ArrayList<>();
和
items.add(name);
致:
items.add(new RoomModel(roomid, name));
试试这个:
ListView listView = (ListView) findViewById(R.id.ChatlistView);
RoomAdapter mArrayAdapter = new RoomAdapter(context,items)
listView.setAdapter(mArrayAdapter);
在您的类RoomModel中添加此构造函数:
public RoomModel(String roomid, String name)
{
this.roomid = roomid;
this.name = name;
}
答案 1 :(得分:0)
在您的代码中
items.add(name);
此处您只在arraylist
中添加名称,因此您只能在listView
中获取名称。要获得房间名,roomId会设置Roommodel类的arraylist
ArrayList<Roommodel> items = new ArrayList<Roommodel>();
并将这些对象添加到列表
items.add(room1);