簡體   English   中英

如何從hashmap用2 textview設置listview

[英]how to set listview with 2 textview from hashmap

AutoSearchDetails.java

雖然是初學者,但從mysql獲取值以創建列表視圖,所以請幫助我

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”}]

listitem.xml

 <?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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM