簡體   English   中英

Android JSON數據未解析為ListView

[英]Android JSON Data not parsing into ListView

Android未將JSON數據解析為ListView,我正在使用本教程,並且對ListViewAdapter.java進行了一些更改

就像在我的新實現中一樣,我使用了ViewHolder,我的代碼如下所示:

 public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();
    ViewHolder holder;

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        public ViewHolder(View convertView) {
            // TODO Auto-generated constructor stub
        }                
        TextView rank;
        TextView country;
        TextView population;
        ImageView flag;
    }  


    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
         // Avoid unneccessary calls to findViewById() on each row, which is expensive!
        holder = null;

        /*
         * If convertView is not null, we can reuse it directly, no inflation required!
         * We only inflate a new View when the convertView is null.
         */

        if (convertView == null) {
            convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);

            // Create a ViewHolder and store references to the two children views
            holder = new ViewHolder(convertView);
            holder.rank = (TextView) convertView.findViewById(R.id.rank);
            holder.country = (TextView) convertView.findViewById(R.id.country);
            holder.population = (TextView) convertView.findViewById(R.id.population);
            // Locate the ImageView in listview_item.xml
            holder.flag = (ImageView) convertView.findViewById(R.id.flag);

            // The tag can be any Object, this just happens to be the ViewHolder
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Capture position and set results to the TextViews
        holder.rank.setText(resultp.get(MainActivity.RANK));
        holder.country.setText(resultp.get(MainActivity.COUNTRY));
        holder.population.setText(resultp.get(MainActivity.POPULATION));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
        // Capture ListView item click

        return convertView;
    }

}

編輯:單擊ListItem代碼

@Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();

            listview.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // code to handle click
                 }
            });
        }

但是我不為什么我不將數據輸入到ListView中!

問題是您沒有將HashMap分配給具有要顯示信息的`resultp。

public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;

    if (convertView == null) {
        convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);

        holder = new ViewHolder(convertView);
        holder.rank = (TextView) convertView.findViewById(R.id.rank);
        holder.country = (TextView) convertView.findViewById(R.id.country);
        holder.population = (TextView) convertView.findViewById(R.id.population);
        holder.flag = (ImageView) convertView.findViewById(R.id.flag);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Here's the change

    resultp    =    data.get(position);      

    // Here's the change

    holder.rank.setText(resultp.get(MainActivity.RANK));
    holder.country.setText(resultp.get(MainActivity.COUNTRY));
    holder.population.setText(resultp.get(MainActivity.POPULATION));
    imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
    return convertView;
}

要將OnItemClickListener附加到ListView ,在包含ListViewActivity中,添加以下內容:

public class MyActivity implements OnItemClickListener{

    ListView  lv;

    @Override
    public void onCreate(Bundle savedInstanceState() {
        ....
        ....
        // lv initialized here
        // adapter of lv set here
        attachListeners();
    }

    private void attachListeners() {
        ....
        ....
        // attach listeners to other views if you like
        lv.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // code to handle click
    }
}

或者,如果您不希望Activity實現OnItemClickListener

public class MyActivity {

    ListView  lv;

    @Override
    public void onCreate(Bundle savedInstanceState() {
        ....
        ....
        // lv initialized here
        // adapter of lv set here
        attachListeners();
    }

    private void attachListeners() {
        ....
        ....
        // attach listeners to other views if you like
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // code to handle click
            }
        });
    }        
}

首先嘗試解決此問題:

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

暫無
暫無

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

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