栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用baseadapter将获取的json数据显示到listview中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用baseadapter将获取的json数据显示到listview中

首先,您不需要创建如下

row_listitem.xml
文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="80dp"android:background="@drawable/list_selector"android:orientation="vertical"android:padding="5dp" ><ImageView    android:id="@+id/iv_icon_social"    android:layout_width="60dp"    android:layout_height="60dp"    android:layout_centerVertical="true"    android:background="@drawable/image_border"    android:src="@drawable/sms_t"    android:visibility="gone" /><LinearLayout    android:id="@+id/thumbnail"    android:layout_width="fill_parent"    android:layout_height="85dp"    android:layout_marginRight="50dp"    android:layout_marginTop="0dp"    android:layout_toRightOf="@+id/iv_icon_social"    android:gravity="center_vertical"    android:orientation="vertical"    android:padding="5dip"    android:visibility="visible" >    <TextView        android:id="@+id/txt_ttlsm_row"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:paddingLeft="10dp"        android:text="Sample text"        android:textSize="18dp"        android:textStyle="bold" />    <TextView        android:id="@+id/txt_ttlcontact_row2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="0dp"        android:layout_marginTop="3dp"        android:paddingLeft="10dp"        android:maxEms="20"        android:maxLines="2"        android:singleLine="false"        android:ellipsize="end"        android:text="Sample text2"        android:textColor="#808080"        android:textSize="15dp"        android:textStyle="normal"        android:visibility="visible" /></LinearLayout></RelativeLayout>

现在,您需要创建 Custom baseAdapter, 如下所示:

  public class baseAdapter2 extends baseAdapter {private Activity activity;// private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;private static ArrayList title,notice;private static LayoutInflater inflater = null;public baseAdapter2(Activity a, ArrayList b, ArrayList bod) {    activity = a;    this.title = b;    this.notice=bod;    inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE);}public int getCount() {    return title.size();}public Object getItem(int position) {    return position;}public long getItemId(int position) {    return position;}public View getView(int position, View convertView, ViewGroup parent) {    View vi = convertView;    if (convertView == null)        vi = inflater.inflate(R.layout.row_listitem, null);    TextView title2 = (TextView) vi.findViewById(R.id.txt_ttlsm_row); // title    String song = title.get(position).toString();    title2.setText(song);    TextView title22 = (TextView) vi.findViewById(R.id.txt_ttlcontact_row2); // notice    String song2 = notice.get(position).toString();    title22.setText(song2);return vi;}

}

现在,您可以设置您的 主要活动, 例如:

public class MainActivity extends Activity {ArrayList<String> title_array = new ArrayList<String>();ArrayList<String> notice_array = new ArrayList<String>();ListView list;baseAdapter2 adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    list = (ListView) findViewById(R.id.listView1);    new TheTask().execute();}class TheTask extends AsyncTask<Void, Void, String> {    @Override    protected String doInBackground(Void... params) {        String str = null;        try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(         "http://10.0.2.2/BSDI/show.php"); HttpResponse response = httpclient.execute(httppost); str = EntityUtils.toString(response.getEntity());        } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();        }        return str;    }@Overrideprotected void onPostExecute(String result) {    super.onPostExecute(result);    String response = result.toString();    try {        JSonArray new_array = new JSonArray(response);        for (int i = 0, count = new_array.length(); i < count; i++) { try {     JSonObject jsonObject = new_array.getJSonObject(i);     title_array.add(jsonObject.getString("title").toString());     notice_array.add(jsonObject.getString("notice").toString()); } catch (JSonException e) {     e.printStackTrace(); }        }        adapter = new baseAdapter2(MainActivity.this, title_array, notice_array);        list.setAdapter(adapter);    } catch (JSonException e) {        // TODO Auto-generated catch block        e.printStackTrace();        // tv.setText("error2");    }}} }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609075.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号