ndroid的ListView控件返回使用baseAdapter

ndroid的ListView控件返回使用baseAdapter

本文介绍了Android的ListView控件返回使用baseAdapter空onItemClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用baseadapter..i正在加载与JSON数据ListView控件从API NW我想我点击列表项的值,并将其发送回data..but它使返回null创建一个ListView ...

I created a listView using baseadapter..i am loading the Listview with a JSON data from an API nw i want to get the value of the list item i click and send it back to the data..but it keeps returning null...

这是BaseAdapter类

this is the BaseAdapter Class

import java.util.ArrayList;
import java.util.HashMap;

import com.nairation.library.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SectionListAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public Typeface rf;
    private Context context;

    public SectionListAdapter(Activity a, ArrayList<HashMap<String, String>> d, Context c){
        activity = a;
        data = d;
        context = c;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.section_list_row, null);
            holder = new ViewHolder();
            holder.topic_name = (TextView) convertView.findViewById(R.id.section_topic_title);
            holder.topic_time = (TextView) convertView.findViewById(R.id.timerelative);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> tpl = new HashMap<String, String>();
        tpl = data.get(position);

        holder.topic_name.setText(tpl.get(SectionList.TITLE));
        holder.topic_time.setText(tpl.get(SectionList.TIME));
        return convertView;
    }

    static class ViewHolder{
        TextView topic_name;
        TextView topic_time;
            public String toString() {
            return "name=" + topic_name;
    }
    }
}

这就是onItemClick监听器,我使用来测试它是否会奏效

and this is the onItemClick listener that i am using to test if it will work

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    Object th = list.getItemAtPosition(position);
    ViewHolder tpi = (ViewHolder) th;
    Toast.makeText(getApplicationContext(), ""+tpi, Toast.LENGTH_SHORT).show();
}

任何帮助球员

推荐答案

我觉得你的问题是,你不执行的getItem 在适配器。它应该是

I think your problem is that you don't implement getItem in your adapter. It should be

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

此外, getItemAtPosition 将返回您的项目之一,在的ArrayList ,这是的HashMap&LT ;字符串,字符串&GT; 。因此,该行应

Also, getItemAtPosition will return one of your items in ArrayList, which is HashMap<String, String>. So the line should be

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    HashMap<String, String> item = (HashMap<String, String>) list.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(), item.get(SectionList.TITLE), Toast.LENGTH_SHORT).show();
}

这篇关于Android的ListView控件返回使用baseAdapter空onItemClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:14