我使用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();
}
任何帮助家伙