Android 如何在json对象生成的listview上添加add AS FRIEND按钮
嗨,我有一个从json对象派生出来的人的列表,这些人被放在一个列表视图中,如下所示。我想在名字前面放一个add按钮,而不是进行OnSelect并将其移动到一个新活动 我该怎么做 下面是我如何将数据生成到listview的。我可以从定义行的xml中添加一个按钮。但是如何放置,或者更确切地说如何对button.onclick进行编码Android 如何在json对象生成的listview上添加add AS FRIEND按钮,android,listview,Android,Listview,嗨,我有一个从json对象派生出来的人的列表,这些人被放在一个列表视图中,如下所示。我想在名字前面放一个add按钮,而不是进行OnSelect并将其移动到一个新活动 我该怎么做 下面是我如何将数据生成到listview的。我可以从定义行的xml中添加一个按钮。但是如何放置,或者更确切地说如何对button.onclick进行编码 ArrayList<HashMap<String, String>> friendslist = new ArrayList<HashMa
ArrayList<HashMap<String, String>> friendslist = new ArrayList<HashMap<String, String>>();
JSONArray jArray = new JSONArray(result);
if (jArray != null) {
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
a = json_data.getString("user_id");
map.put(TAG_Name, json_data.getString("fname"));
map.put(TAG_LName, json_data.getString("lname"));
friendslist.add(map);
}
}
list = (ListView) findViewById(R.id.addfrndslist);
ListAdapter adapter = new SimpleAdapter(this, friendslist,
R.layout.add_yourfriendswith, new String[] { TAG_Name,
TAG_LName}, new int[] { R.id.afname,
R.id.alname});
list.setAdapter(adapter);
ArrayList friendslist=new ArrayList();
JSONArray jArray=新JSONArray(结果);
if(jArray!=null){
for(int i=0;i
您必须创建自定义列表适配器才能将按钮添加到列表行中
把这个写下来
final ListView itemlist = (ListView) findViewById(R.id.itemlist);
itemlist.setAdapter(new PodcastListAdapter(this));
然后创建自定义BaseAdapter类
class ViewHolder {
TextView txtTitle;
TextView txtDescription;
Button btnDownload;
}
public class PodcastListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
public PodcastListAdapter(Context cont) {
mInflater = LayoutInflater.from(cont);
context = cont;
}
public int getCount() {
return Utilities.arrayRSS.size();
}
public Object getItem(int position) {
return Utilities.arrayRSS.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.txtDescription = (TextView) convertView
.findViewById(R.id.description);
holder.btnDownload = (Button) convertView
.findViewById(R.id.btnDownload);
holder.btnDownload.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new DownloadingProgressTask(position, holder).execute();
holder.btnDownload
.setBackgroundResource(R.drawable.downloading);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(Utilities.arrayRSS.get(position).getTitle());
holder.txtDescription.setText(Utilities.arrayRSS.get(position)
.getDescription());
return convertView;
}
对于列表视图,您使用的是android提供的SimpleAdapter,但这仅在您必须显示名称列表时才有效。如果你想根据你的需要定制你的列表,比如在每一行上添加按钮或图像等,那么你必须制作你自己的适配器。
以下是执行此操作的步骤:
行项目的XML
以下是单击“添加”按钮时调用的方法
public void addFriend(View v) {
int position = Integer.parseInt(v.getTag().toString());
//now you have the position of the button that you can use for your purpose
}
以下是设置适配器的代码
//String array of names
String[] names;
MyListAdapter mAdapter = new MyListAdapter(names,yourActivity.this);
list.setAdapter(mAdapter);
我希望这会有所帮助:)
按钮是如何从JSON对象生成的?我可以向行中添加一个按钮,单击该按钮可以添加它。但不知道如何以及在何处对Button.onclick MethodHnx进行编码,它位于my list=(ListView)findViewById(R.id.addfrndslist)之后;ListAdapter=new SimpleAdapter(this,friendslist,R.layout.add_yourfriendswith,新字符串[]{TAG_Name,TAG_LName},新int[]{R.id.afname,R.id.alname});list.setAdapter(适配器);对吗?以上几行你不必写,请搜索如何在android中创建自定义Listview。如果你在拒绝回答之前给出适当的理由,这将是非常有帮助的,这样我可以改进我的答案。
public void addFriend(View v) {
int position = Integer.parseInt(v.getTag().toString());
//now you have the position of the button that you can use for your purpose
}
//String array of names
String[] names;
MyListAdapter mAdapter = new MyListAdapter(names,yourActivity.this);
list.setAdapter(mAdapter);