0

こんにちは私はjsonオブジェクトから派生し、以下のようにリストビューに入れられた人々のリストを持っています。名前の前に追加ボタンを配置したいのですが、それを選択して新しいアクティビティに移動します。

どうやってやるの?

以下は、リストビューにデータを生成する方法です。行を定義する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);
4

2 に答える 2

0

ボタンをリスト行に追加するには、カスタムリストアダプタを作成する必要があります。

これを書いてください

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;
}
于 2012-07-23T04:58:14.847 に答える
0

リストビューには、Androidが提供するSimpleAdapterを使用していますが、これは名前のリストを表示する必要がある場合にのみ機能します。各行にボタンや画像などを追加するなど、必要に応じてリストをカスタマイズする場合は、独自のアダプターを作成する必要があります。
これを行う手順は次のとおりです。

  1. BaseAdapterまたはArrayAdapterを拡張してカスタムアダプタを作成します
  2. そのメソッドをオーバーライドします。

注:リストビューの行にクリック可能なアイテムを追加すると、そのアイテムはクリック可能になりますが、リストはクリックできなくなります。したがって、リストonItemClickListenerは機能しません。この場合、クリックされたアイテムの位置を取得するには、いくつかの回避策を実行する必要があります。行アイテムでsetTag()を使用し、getTag()メソッドを使用して、グループ内の位置となるタグを取得します。

このためのサンプルコードは次のとおり
です。行アイテムの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);

これがお役に立てば幸いです:)

于 2012-07-24T12:44:24.313 に答える