嗨我有一个从json对象派生的人员列表,并按照下面的方式放入列表视图。我想在名称前面添加一个添加按钮,而不是进行onslect并将其移动到新活动。
我该怎么做?
以下是我如何将我的数据发布到列表视图中。我可以从xml中添加一个按钮来定义行。但是如何设置或者如何编码button.onclick?
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);
答案 0 :(得分:0)
您必须创建自定义列表适配器才能将Button添加到列表行。
将此内容写入
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;
}
答案 1 :(得分:0)
对于列表视图,您使用的是Android提供的SimpleAdapter,但仅当您必须显示名称列表时才有效。如果您希望根据需要自定义列表,例如在每行上添加按钮或图像等,则必须自己制作适配器。
以下是执行此操作的步骤:
注意:如果您将任何可点击的项目添加到列表视图行,则该项目将是可点击的,但列表将不可点击。因此列表onItemClickListener将不起作用。现在,在这种情况下,为了获得项目的位置,你必须做一些解决方法。我在行项目和getTag()方法上使用setTag()来获取它在组中的位置。
以下是此示例代码:
行项目的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/nameTxt"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="3"
/>
<Button
android:id="@+id/addBtn"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Add"
android:onClick="addFriend"
/>
</LinearLayout>
自定义适配器代码
public class MyListAdapter extends BaseAdapter
{
String names[];
LayoutInflater mInflater;
public MyListAdapter(String _names[],Context context) {
names = _names;
mInfalter = LayoutInflater.from(context);
}
@Override
public int getCount ( )
{
return names.length;
}
@Override
public Object getItem (int position)
{
return names[position];
}
@Override
public long getItemId (int position)
{
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = mInfalter.inflate(R.layout.list_item, null);
holder.name = (ImageView)convertView.findViewById(R.id.name);
holder.addBrn = (TextView)convertView.findViewById(R.id.addBtn);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.templeName.setText(names[position]);
holder.addBtn.setTag(Integer.toString(position));
return convertView;
}
static class ViewHolder {
TextView name;
Button addBtn;
}
}
这是单击添加按钮时调用的方法
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);
我希望这会有所帮助:)