2

I am converting a working application to Fragments. My checkbox list is not displaying the labels, and I am guessing, the id's. I am new to Android/Java and this is my first post to Stackoverflow, so apologies if the question is simple or not following correct protocol.

My ListFragment is as follows:

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();

        }
    });
}

} 

My XML for the list is as follows:

<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>

My XML for the fragment is as follows:

<?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>

Any help would be greatly appreciated.

0

1 Answer 1

2

I nutted it out, so thought I would post my answer in case others have the same problem. It was my context. It should have been:

mContext = getActivity();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.