从mysql获取值以创建listview虽然我是初学者所以请帮助我
protected void onPostExecute(String result) {
Log.d("jason got", result);
JSONArray jarray;
try {
jarray = new JSONArray(result);
for(int i=0;i<jarray.length();i++){
HashMap<String, String> hm;
hm=new HashMap<String, String>();
JSONObject jobj=jarray.getJSONObject(i);
hm.put("Name",jobj.getString("name"));
hm.put("Phone",jobj.getString("phone"));
Log.d("name", jobj.getString("name"));
oslist.add(hm);
}
} catch (JSONException e) {
Toast.makeText(getBaseContext(), "Not getting result", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Log.d("oslist", oslist.toString());
ListAdapter adapter = new SimpleAdapter(AutoSearchDetails.this, oslist,R.layout.listitem,new String[] { Name,Phone }, new int[] {
R.id.name,R.id.phone});
Log.d("adapter", adapter.toString());
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(
AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(AutoSearchDetails.this, "You Clicked at "+oslist.get(+arg2).get("name"), Toast.LENGTH_SHORT).show();
}
});
};
在日志中我得到“oslist”[{“name:fe”,“phone:32”}]
<?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/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
问题是值没有设置到listview
答案 0 :(得分:-1)
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private final ArrayList mData;
public MyAdapter(Map<String, String> map) {
mData = new ArrayList();
mData.addAll(map.entrySet());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Map.Entry<String, String> getItem(int position) {
return (Map.Entry) mData.get(position);
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent, false);
} else {
result = convertView;
}
Map.Entry<String, String> item = getItem(position);
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue());
return result;
}
}
布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
代码中的适配器
public void showCinemas(HashMap<String, String> cinemas) {
MyAdapter adapter = new MyAdapter(cinemas);
list.setAdapter(adapter);
}