Android ListView returning null onItemClick using baseAdapter

有些话、适合烂在心里 提交于 2019-12-08 06:13:26

问题


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

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

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

any help guys


回答1:


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

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



回答2:


You could instead use the built-in Android adapter called SimpleAdapter:

         SimpleAdapter(Context context,List<HashMap<String,String>>,int resourceId,String from,int to) 


来源:https://stackoverflow.com/questions/19193536/android-listview-returning-null-onitemclick-using-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!