1

我使用baseadapter创建了一个listView ..我正在使用来自API nw的JSON数据加载Listview 我想获取我单击的列表项的值并将其发送回数据..但它一直返回null ...

这是 BaseAdapter 类

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 侦听器

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

任何帮助家伙

4

2 回答 2

3

我认为您的问题是您没有getItem在适配器中实现。它应该是

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

此外,getItemAtPosition将退回您的一件物品ArrayList,即HashMap<String, String>. 所以这条线应该是

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();
}
于 2013-10-05T03:34:28.450 回答
0

您可以改用名为 SimpleAdapter 的内置 Android 适配器:

         SimpleAdapter(Context context,List<HashMap<String,String>>,int resourceId,String from,int to) 
于 2013-10-25T07:31:18.993 回答