我已经按照本教程进行操作,但我无法弄清楚为什么模拟器没有显示这些项目。
这是我的列表活动:
public class MyListActivity extends ListActivity {
private Timer myTimer;
private boolean SetTimerOnResume=false;
private ArrayList<HashMap<String,String>> chat=new ArrayList<HashMap<String,String>>();
private LazyAdapter adapter;
static final String Key_username="username_key";
static final String Key_email="email_key";
static final String Key_messageText="messageText_key";
static final String Key_messageDate="messageDate_key";
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.lists);
HttpRequest httpRequest=new HttpRequest("ReturnNewChatsToAdmin", "admin", "You" , "zawalarsa", "muazzamalii@hotmail.com");
String data=httpRequest.ExecuteRequest();
httpRequest.ExecuteRequest();
try {
JSONArray jsonarr=new JSONArray(data);
for(int i=0;i<jsonarr.length();i++)
{
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put(MyListActivity.Key_username, jsonarr.getJSONObject(i).getString("username"));
hashMap.put(MyListActivity.Key_email, jsonarr.getJSONObject(i).getString("email"));
hashMap.put(MyListActivity.Key_messageText, jsonarr.getJSONObject(i).getString("chatText"));
hashMap.put(MyListActivity.Key_messageDate, jsonarr.getJSONObject(i).getString("chatDate"));
chat.add(hashMap);
}
ListView lv=(ListView)findViewById(android.R.id.list);
adapter=new LazyAdapter(this,chat);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
}
});
}
catch(JSONException e)
{
}
}
}
这是教程中所示的lazyadapter:
包 com.app.ServerClient;
公共类 LazyAdapter 扩展 BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String,String>> hashmap;
private static LayoutInflater inflater=null;
public LazyAdapter(Activity activity,ArrayList<HashMap<String,String>> hashMaps)
{
this.activity=activity;
this.hashmap=hashMaps;
LazyAdapter.inflater=(LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return hashmap.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(convertView==null)
view=inflater.inflate(R.layout.list_layout,null);
TextView username=(TextView)view.findViewById(R.id.username);
TextView email=(TextView)view.findViewById(R.id.email);
TextView messageDate=(TextView)view.findViewById(R.id.chatdate);
TextView messagetext=(TextView)view.findViewById(R.id.messagetext);
HashMap<String,String> map=new HashMap<String, String>();
hashmap.get(position);
username.setText(map.get(MyListActivity.Key_username));
email.setText(map.get(MyListActivity.Key_email));
messageDate.setText(map.get(MyListActivity.Key_messageDate));
messagetext.setText(map.get(MyListActivity.Key_messageText));
return view;
}
}
这是我的 list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="11.74"
android:textColor="#FFFF00"
android:textSize="17sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/chatdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="11.74"
android:textColor="#FFFF00"
android:textSize="17sp"
android:textStyle="bold"
/>
</LinearLayout>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Message"
android:textStyle="bold"/>
<TextView android:id="@+id/messagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
这是输出: