android-arrayadapter - 安卓 : implementing Checkbox List with ListFragment and ArrayAdapter

标签 android-arrayadapter android-listfragment android-checkbox

我正在将工作应用程序转换为片段。我的复选框列表没有显示标签,我猜是 id 的。我是 Android/Java 新手,这是我在 Stackoverflow 上发表的第一篇文章,所以如果问题很简单或没有遵循正确的协议(protocol),我深表歉意。

我的ListFragment如下:

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class DistributionTransFragment extends ListFragment {

protected TextView subheader;
protected Context mContext;
protected DatabaseHandler db;
protected String user_id;
protected String user;
protected String recipients;
protected int number;
protected LayoutInflater inflater;
protected View v;

DistributionArrayAdapter dataAdapter = null;

public DistributionTransFragment(){
    super();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.distribution_fragment, container, false);

    mContext = getActivity().getApplicationContext();

    db = new DatabaseHandler(mContext);

    subheader = (TextView)v.findViewById(R.id.textView2);
    subheader.setText("Spent On?"); 

    displayListView();
    checkButtonClick();

    return v;   
}

private void displayListView() {

    ArrayList<Participant> distributionList = new ArrayList<Participant>();

    Participant participant;

    List<User> users = db.getAllUsers();       

    for (User u : users) {

        user_id = String.valueOf(u.getID());
        user = u.getUser();

        participant = new Participant(user_id, user, false);
        distributionList.add(participant);

    } 

    //create an ArrayAdaptar from the String Array
    dataAdapter = new DistributionArrayAdapter(mContext, R.layout.distributionlist, distributionList);

    setListAdapter(dataAdapter);

}



private class DistributionArrayAdapter extends ArrayAdapter<Participant> {

    private ArrayList<Participant> distributionList;

    public DistributionArrayAdapter(Context context, int textViewResourceId, ArrayList<Participant> distributionList) {
        super(context, textViewResourceId, distributionList);
        this.distributionList = new ArrayList<Participant>();
        this.distributionList.addAll(distributionList);
    }

    private class ViewHolder {

        TextView code;
        CheckBox name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.distributionlist, null);
            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
            convertView.setTag(holder);
            Log.d("HOLDER CODE", (holder.code).toString()); 
            holder.name.setOnClickListener( new View.OnClickListener() {

                public void onClick(View v) {  

                    CheckBox cb = (CheckBox) v ;  
                    Participant participant = (Participant) cb.getTag();  
                    participant.setSelected(cb.isChecked());

                }  
            });  

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        Participant participant = distributionList.get(position);
        holder.name.setText(participant.getName());
        holder.name.setChecked(participant.isSelected());
        holder.name.setTag(participant);

        return convertView;

    }  

}

private void checkButtonClick() {

    Button myButton = (Button) v.findViewById(R.id.button1);

    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            final ArrayList<Participant> distributionList = dataAdapter.distributionList;

            // Do Stuff             


            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setMessage(tempTransaction);
            builder.setCancelable(false);
            builder.setPositiveButton("Commit", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {


                    // Do More Stuff 
                                            Intent intent = new Intent(mContext, Home.class);
                    startActivity(intent);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do Other Stuff
                    Intent intent = new Intent(mContext, Home.class);
                    startActivity(intent);
                }
            });
            AlertDialog alert = builder.create();
            alert.show();

        }
    });
}

} 

我的列表 XML 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
android:padding="15dip" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false" />

<TextView
    android:id="@+id/code"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_toRightOf="@+id/checkBox1"
    android:textSize="15sp" />

</RelativeLayout>

我的片段 XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="@color/grey"> 

<RelativeLayout
    android:id="@+id/subheader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/secondheader"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        style="@style/subheader" />

    <TextView
        android:id="@+id/textView3"
        style="@style/info" />

</RelativeLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/subheader"
    android:layout_above="@+id/footer"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:id="@id/footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/grey"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/button1"
        style="@style/button.form"
        android:text="@string/moneyspent" />
</LinearLayout>

</RelativeLayout>

任何帮助将不胜感激。

最佳答案

我把它搞砸了,所以我想我会发布我的答案,以防其他人遇到同样的问题。这是我的背景。本来应该是:

mContext = getActivity();

关于android-arrayadapter - 安卓 : implementing Checkbox List with ListFragment and ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15016419/

相关文章:

android - ListFragment NullPointerException 错误

android - ArrayAdapter 、 BaseAdapter 和 ListAdapter 有什么区别

java - 从细节部分刷新Android Master/Detail Flow的Master page

android - 如何在android中的gridview滚动中保留复选框状态

android - 自定义适配器 ListView,setOnCheckedChangeListener 不工作

Listview 的 ArrayAdapter notifydatasetchanged() 重绘很慢

android - 牛轧糖上深色背景上的黑色文字(android.R.layout.simple_spinner_item)

android - 使用 ViewPager 创建主/从流程

android - ListFragment给出id属性为 'android.R.id.list'的ListView

Android使用按钮和对话框复选框进行多项选择,并在 ListView 中显示所选项目而不是按钮