Button below listView
I am new to Android and the first project I am working on is putting players on a team. I have a list that grabs all players from the database. Each name should have a checkbox next to it and then there is a button at the bottom that should update the database. So far this is what I get.
Here is my activity
package com.example.sqllitetest;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Tester extends ListActivity {
MyAdapter mListAdapter;
PlayersDBAdapter mDbHelper;
private Button button;
private Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new PlayersDBAdapter(this);
mDbHelper.open();
Cursor myCur = mDbHelper.fetchAllPlayers();
startManagingCursor(myCur);
setContentView(R.layout.playertoteam);
// registerForContextMenu(getListView());
// ((Tester) context).setContentView(R.layout.playertoteam);
//getListView().addFooterView(button);
mListAdapter = new MyAdapter(Tester.this, myCur);
setListAdapter((ListAdapter) mListAdapter);
Button button = (Button) findViewById(R.id.alertBox);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.playertoteam, cur);
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.playertoteam, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.code);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.checkBox1);
tvListText.setText(cur.getString(cur.getColumnIndex(mDbHelper.KEY_NAME)));
cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(mDbHelper.KEY_ID))==1? false:true));
}
}
}
And here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<Button
android:id="@+id/alertBox"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/confirm" />
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/alertBox"
/>
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
</LinearLayout>
I would like to know how to remove buttons from rows and a checkbox that is under the main confirm button. Also I would like to know if this is the best way to do something like this, because, again, I am a beginner and I do not know how to look for the correct methods.
source to share
The ResourceCursorAdapter creates views for each row of the ListView, which are inflated with the newView () method. For this reason, you get a button on each line. Soln: Create a separate XML array defining the row layout for the ListView and inflate this new layout in the newView () method. And have only button and ListView in main layout i.e. playertoteam.xml
playertoteam.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="wrap_content"
android:padding="6dip" >
<Button
android:id="@+id/alertBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/confirm" />
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@id/alertBox" />
</RelativeLayout>
row_layout.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="wrap_content">
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
</RelativeLayout>
You can find an example for ListView here
source to share