I have an activity that get data (arraylist) from a previous activity and put it in a listview through a custom adapter. In this custom adapter i declare a textview (to show the data) and add a numberpicker for each row.
I want the user to be able to select a number (with numberpicker) and when he has finished, to click on a button (fini) that display data with its numberpicker value in a textview.
My activity code (choozQr3.java):
public class ChoozQr3 extends Activity {
Button fini;
ListView lv;
TextView logQr;
Context context;
ChoozQr3Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choozqr_step3);
fini = (Button) findViewById(R.id.fini);
lv = (ListView) findViewById(R.id.listView1);
logQr = (TextView) findViewById(R.id.textView2);
Bundle b = getIntent().getExtras();
String[] sTemp = b.getStringArray("arrangedItems");
ArrayList<String> resultArr = new ArrayList<String>(Arrays.asList(sTemp));
adapter = new ChoozQr3Adapter(this, R.layout.choozqr_step3_textview, resultArr);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
fini.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//ArrayList<String> finalItems = new ArrayList<String>();
for (int i = 0; i < adapter.getCount(); i++) {
Toast.makeText(getApplicationContext(),i+". " +adapter.getItem(i).toString(),Toast.LENGTH_LONG).show();
//finalItems.add(adapter.getItem(i).toString());
}
}
}
);
}
}
Here is the code of my customadapter :
public class ChoozQr3Adapter extends ArrayAdapter<String>{
private final List<String> list;
public ChoozQr3Adapter(Context context, int resource, List<String> items) {
super(context, resource, items);
list = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.choozqr_step3_textview, parent, false);
//ContentValues cv = new ContentValues();
}
TextView tvNomDuQr=(TextView)v.findViewById(R.id.NomQr);
NumberPicker npNbJours=(NumberPicker)v.findViewById(R.id.numberPickerOccurence);
tvNomDuQr.setText(list.get(position));
npNbJours.setMaxValue(365);
npNbJours.setMinValue(1);
npNbJours.setValue(1);
npNbJours.setWrapSelectorWheel(true);
return v;
}
}
And the XML related to the adapter :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/NomQr"
android:layout_width="400dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<NumberPicker
android:id="#+id/numberPickerOccurence"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignTop="#+id/NomQr"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="#+id/textView1"
android:orientation="horizontal" />
<TextView
android:id="#+id/textView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:text="jours"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Until now, i managed to do it with setOnItemClickListener as described in this code (from activity choozQr3.java) :
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3){
// when user clicks on ListView Item , onItemClick is called
// with position and View of the item which is clicked
// we can use the position parameter to get index of clicked item
TextView tvNomDuQr=(TextView)v.findViewById(R.id.NomQr);
NumberPicker npNbJours=(NumberPicker)v.findViewById(R.id.numberPickerOccurence);
String NomDuQr=tvNomDuQr.getText().toString();
Integer NbJours=npNbJours.getValue();
// Show The result
Toast.makeText(getApplicationContext(),"répéter "+NbJours+" fois le questionnaire "+NomDuQr,Toast.LENGTH_LONG).show();
}
});
But i don't know how to modify my fini.setOnClickListener () to achieve same result...
Any help would be greatly appreciated...
Finally find the solution by myself.
public void onClick(View v) {
ArrayList<String> QrEtOccurence = new ArrayList<String>();
TextView tvNomDuQr;
NumberPicker npNbJours;
Integer j = 0;
for (int i = 0; i < lv.getCount(); i++) {
v = lv.getChildAt(i);
tvNomDuQr=(TextView)v.findViewById(R.id.NomQr);
npNbJours=(NumberPicker)v.findViewById(R.id.numberPickerOccurence);
String NomDuQr=tvNomDuQr.getText().toString();
Integer NbJours=npNbJours.getValue();
String output = "Présenter durant "+NbJours+" jours le questionnaire "+NomDuQr;
QrEtOccurence.add(output);}
String[] outputStrArr = new String[QrEtOccurence.size()];
for (int i = 0; i < QrEtOccurence.size(); i++) {
outputStrArr[i] = QrEtOccurence.get(i);}
}
Related
I am writing the basic task management application. I am using base adapter to work with ListView. I wonder how I can remove (a few) checked values from the list? And how I can remove only one value with long click?
if this posible with my adapter? I tried a few options but nothing worked.
Here is my code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/btn1"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:text="\u2713"
android:background="#drawable/add_btn"
android:onClick="knopka2"/>
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/btn1"
android:hint="put your task here"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:divider="#drawable/deriver"
android:layout_below="#+id/input"
android:layout_centerHorizontal="true"
android:choiceMode="multipleChoice" />
<TextView
android:id="#+id/ttl"
android:layout_below="#id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/tryClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ttl"
android:onClick="tryClear"
/>
</RelativeLayout>
This is my Activity:
public class Trird extends AppCompatActivity {
Button btn;
EditText input4;
TextView ttl;
ListView list;
public static ArrayList<String> taskList = new ArrayList<String>();
SparseBooleanArray sparseBooleanArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
input4 = (EditText) findViewById(R.id.input);
btn = (Button) findViewById(R.id.btn1);
list = (ListView) findViewById(R.id.listView);
ttl = (TextView)findViewById(R.id.ttl);
}
public String[] putToArray() {
String ag = input4.getText().toString().trim();
if (ag.length() != 0) {
taskList.add(ag);
input4.setText("");
}
String[] abc = taskList.toArray(new String[taskList.size()]);
return abc;
}
public void knopka2(View v) {
final String[] ListViewItems = putToArray(); //
list = (ListView) findViewById(R.id.listView);
list.setAdapter(new MyAdapter(ListViewItems, this));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View tv, int i, long id) {
///tv.setBackgroundColor(Color.YELLOW);
///ttl.setText("selected: " + list.getAdapter().getItem(i));
sparseBooleanArray = list.getCheckedItemPositions();
String ValueHolder = "";
int a = 0;
while (a < sparseBooleanArray.size()) {
if (sparseBooleanArray.valueAt(a)) {
ValueHolder += ListViewItems[sparseBooleanArray.keyAt(a)] + ",";
}
a++;
}
ValueHolder = ValueHolder.replaceAll("(,)*$", "");
Toast.makeText(Trird.this, "ListView Selected Values = " + ValueHolder, Toast.LENGTH_LONG).show();
if(ValueHolder!=null){
taskList.remove(ValueHolder);
}
}
});
}
}
This is my adapter:
public class MyAdapter extends BaseAdapter {
private final Context context;//Context for view creation
private final String[] data;//raw data
public MyAdapter(String[] data, Context context){
this.data=data;
this.context=context;
}
//how many views to create
public int getCount() {
return data.length;
}
//item by index (position)
public Object getItem(int i) {
return data[i];
}
//ID => index of given item
public long getItemId(int i) {
return i;
}
//called .getCount() times - for each View of item in data
public View getView(int i, View recycleView, ViewGroup parent) {
if(recycleView==null)recycleView = LayoutInflater.from(context).inflate(R.layout.item,null);
((TextView)recycleView).setText(data[i]);//show relevant text in reused view
return recycleView;
}
}
And this is how looks like item in ListView.
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
/>
The problem is that you are trying removing item using the ListView remove methode.
Just remove the selected item from the list using the remove() method of your Adapter.
A possible way to do that would be:
CheckedTextView checkedText= baseAdapter.getItem([POSITION OF THE SELECTED ITEM]);
baseAdapter.remove(checkedText);
baseAdapter.notifyDataSetChanged();
put this code inside the wanted button click and there you go.
Am developing an app that take student attendance, i want to get the list of checked the names on "TakeAttendance" when i click a "ViewAttendance" on same custom listview . Please how can i do that?
here is my codes...
The custom view
enter code here
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_firstname"
android:text="Firstname"
android:textSize="11dp"
android:ellipsize="start"
android:lines="3"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:paddingLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lastname"
android:lines="3"
android:textSize="11dp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:paddingLeft="10dp"
android:id="#+id/tv_lastname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_surname"
android:text="Surname"
android:lines="3"
android:textSize="11sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:paddingRight="80dp"
android:paddingLeft="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="20dp"
android:id="#+id/checkBox"
android:checked="false"
android:focusable="false"
android:background="#color/white" />
The Listview
enter code here
The ListAdapter
enter code herepublic class StudentListAdapter1 extends BaseAdapter {
private Context mContext;
private List mStudentList;
//Constructor
public StudentListAdapter1(Context mContext, List<StudentList> mStudentList) {
this.mContext = mContext;
this.mStudentList = mStudentList;
}
#Override
public int getCount() {
return mStudentList.size();
}
#Override
public Object getItem(int position) {
return mStudentList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.student_take, null);
TextView tvReg_no = (TextView) v.findViewById(R.id.tv_reg_no);
TextView tvFirstname = (TextView) v.findViewById(R.id.tv_firstname);
TextView tvLastname = (TextView) v.findViewById(R.id.tv_lastname);
TextView tvSurname = (TextView) v.findViewById(R.id.tv_surname);
//Set text for TextView
tvReg_no.setText(mStudentList.get(position).getReg_no());
tvFirstname.setText(mStudentList.get(position).getFirstname());
tvLastname.setText(mStudentList.get(position).getLasttname());
tvSurname.setText(mStudentList.get(position).getSurname());
//Save product id to tag
v.setTag(mStudentList.get(position).getId());
return v;
}
}
The Attendance Activity
enter code here protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_take100);
lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);
mStudentList = new ArrayList<>();
//Add sample data for list
//We can get data from DB, webservice here
mStudentList.add(new StudentList(1, "U11EE1001", "Bargo","S.","Mayafi"));
mStudentList.add(new StudentList(2, "U11EE1002", "Barnsbas","Snake.","Maciji"));
mStudentList.add(new StudentList(3, "U11EE1004", "Adamu","Tanko.","Sadau"));
mStudentList.add(new StudentList(4, "U11EE1005", "Munzali","","Cire Tallafi"));
//Init adapter
adapter = new StudentListAdapter1(getApplicationContext(), mStudentList);
lvStudentlist.setAdapter(adapter);
checkBox = (CheckBox) findViewById(R.id.checkBox);
lvStudentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
SparseBooleanArray checked = lvStudentlist.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
position = checked.keyAt(i);
// Add names if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add((String) adapter.getItem(position));
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
ViewAttendanceActivity.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
}
});
}
}
The View Attendance Activity
enter code here protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_attendance);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);
adapter = new StudentListAdapter(getApplicationContext(), mStudentList);
lvStudentlist.setAdapter(adapter);
}
}
At first I think you should reuse your items in listview
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView != null) {
v = convertView;
} else {
v = View.inflate(mContext, R.layout.student_take, null);
}
.......
now answer for your question, I think you need to add your selected items to array in lvStudentlist.setOnItemClickListener method and then send it as Intent extra to your next activity. You can send String[] as Intent extra so you have no need to use Bundle.
I am trying to make a specific (selected by user) row of a ListView checkbox to become visible. But the problem is,I have dynamically added 10 rows in my list view using class that extends ArrayAdapter and when I select the 1st row then along with my 1st row , 3rd , 5th and so on check box is becoming visible. Similar with 2nd, 4th and so on row.
I just want that particular row (eg position 0) that I selected to show up the check box and rest shoulb be invisible.
public class LazyAdapter extends ArrayAdapter<RowItem> {
Context context;
public LazyAdapter(Context context, int resourceId, List<RowItem> items){
super(context, resourceId, items);
this.context = context;
}
public class ViewHolder{
CheckedTextView checkbox;
TextView title;
TextView description;
LinearLayout card;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.card = (LinearLayout) convertView.findViewById(R.id.card);
holder.checkbox = (CheckedTextView)convertView.findViewById(R.id.deliverychecktext);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.description = (TextView)convertView.findViewById(R.id.description);
convertView.setTag(holder);
} else
holder = (ViewHolder)convertView.getTag();
//holder.image.setImageResource(rowItem.getImageId());
holder.title.setText(rowItem.getTitle());
holder.description.setText(rowItem.getDesc());
Animation animation = AnimationUtils.loadAnimation(context, R.anim.card_animation);
holder.card.startAnimation(animation);
return convertView;
}
}
MainActivity class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intialize and set the Action Bar to Holo Blue
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33b5e5" )));
ListView lv = (ListView) findViewById(R.id.myList);
rowItems = new ArrayList<RowItem>();
String[] titles = {"Address1","Address2","Address3","Address4","Address5","Address6","Address7","Address8"};
String[] descriptions = {"First Address","Second Address","Third Address","Fourth Address","Fifth Address",
"Sixth Address","Seventh Address","Eighth Address"};
//Populate the List
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem("Delivery Address :", descriptions[i]);
rowItems.add(item);
}
// Set the adapter on the ListView
LazyAdapter adapter = new LazyAdapter(getApplicationContext(), R.layout.list_row, rowItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedTextView check = (CheckedTextView) view.findViewById(R.id.deliverychecktext);
if (check.getVisibility() == 4)
check.setVisibility(1);
else
check.setVisibility(4);
Toast.makeText(getApplicationContext(), "h u clicked : "+position,
Toast.LENGTH_LONG).show();
}
});
list_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/card_no_border"
android:orientation="vertical"
android:padding="2dp"
android:id="#+id/card">
<CheckedTextView
android:id="#+id/deliverychecktext"
android:layout_width="fill_parent"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:layout_height="wrap_content"
android:checkMark="#drawable/ic_launcher"
android:visibility="invisible"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Dog Tag"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#040404"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Saved Address"
android:textSize="20sp"
android:padding="5dp"/>
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"/>
</LinearLayout>
First one RowItem as class varible to store the selected items.
private RowItem mSelectedItem = null;
ex
public class LazyAdapter extends ArrayAdapter<RowItem> {
Context context;
private RowItem mSelectedItem = null;
After that add the below listener to your LazyAdapter adapter class.
/**
* On Click Listener for the view.
*/
protected class OnViewClickListener implements
android.view.View.OnClickListener {
private RowItem mItem;
public OnViewClickListener(RowItem item) {
mItem = item;
}
#Override
public void onClick(View v) {
mSelectedItem = mItem;
notifyDataSetChanged();
}
}
call this listener from your getView method in LazyAdapter before return view and add logic for the show and hid the view.
ex :
if(mSelectedItem != null && mSelectedItem == rowItem) {
holder.checkbox.setVisibility(View.VISIBLE);
} else {
holder.checkbox.setVisibility(View.INVISIBLE);
}
convertView.setOnClickListener(new OnViewClickListener(rowItem));
return convertView;
}
And remove click lister in ur activity.
on your activity its working because while rendering the adapter view it will reuse the object, so it was happening like that. Now we are checking and updating every time while view rendering.
I'm having a heck of a time making rows from a custom list view made with a baseadapter clickable. Currently, they can be clicked, but an error ensues and the app crashes..
ANY HELP WOULD BE APPRECIATED
Here is the main activity which includes the custom adapter that extends a baseadapter
public class MainActivity extends Activity implements OnItemClickListener {
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
list.setAdapter(new HalsAdapter(this));
list.setOnItemClickListener(this);
}
class SingleRow {
String title;
String description;
int image;
SingleRow(String title, String description, int image) {
this.title = title;
this.description = description;
this.image = image;
}
}
class HalsAdapter extends BaseAdapter {
ArrayList < SingleRow > list;
Context context;
HalsAdapter(Context c) {
context = c;
list = new ArrayList < SingleRow > ();
Resources res = c.getResources();
String[] titles = res.getStringArray(R.array.titles);
String[] descriptions = res.getStringArray(R.array.descriptions);
int[] images = {
R.drawable.placeholder1, R.drawable.placeholder2, R.drawable.placeholder3, R.drawable.placeholder4, R.drawable.placeholder5, R.drawable.placeholder6, R.drawable.placeholder7, R.drawable.placeholder8, R.drawable.placeholder9, R.drawable.placeholder10, R.drawable.placeholder11, R.drawable.placeholder12, R.drawable.placeholder13, R.drawable.placeholder14, R.drawable.placeholder15
};
for (int i = 0; i < 15; i++) {
list.add(new SingleRow(titles[i], descriptions[i], images[i]));
}
}
#Override
public int getCount() {
return list.size();
}#Override
public Object getItem(int i) {
return list.get(i);
}#Override
public long getItemId(int i) {
return i;
}#Override public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewGroup, false);
TextView title = (TextView) row.findViewById(R.id.textView);
TextView description = (TextView) row.findViewById(R.id.textView2);
ImageView image = (ImageView) row.findViewById(R.id.imageView);
SingleRow temp = list.get(i);
title.setText(temp.title);
description.setText(temp.description);
image.setImageResource(temp.image);
return row;
}
}#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int arg2, long arg3) {
Intent intent = new Intent(homeScreen.this, privateSpaceList.class);
startActivity(intent);
break;
}
}
Here is single_row.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"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="#drawable/placeholder1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/textView"
android:layout_alignParentRight="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>`
I left the onitemclick empty because my methods were not working and the mainactivity doesn't like me to call the other method of onitemclick.
I created an activity called ListItemActivity I would like the onclick to call, in turn the listitemactivity calls a test.xml page. I have been stuck for a few days trying to figure out how to make the custom rows clickable.. Any help would be greatly appreciated.
For my listview, I want to be able to display multiple textviews within it, with each textview displaying the items of a specific array associated with it. Right now is what I have:
public class GreatestHits extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Array1));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text = (String) ((TextView)view).getText();
}
});
}
static final String[] Array1 = new String[] {"Item1", "Item2"};
static final String[] Array2 = new String[] {"Item1", "Item2"};
static final String[] Array3 = new String[] {"Item1", "Item2"};
and the xml for R.layout.list_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "#+id/textView1"
>
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "#+id/textView2"
>
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "#+id/textView3"
>
</TextView>
How would I put array 2 and array 3 into the code so it can be displayed in the textviews of listview lv?
ArrayAdapter can only display data from one array in one textview. You will have to provide this functionality yourself by extending BaseAdapter yourself. I can't compile android code at the moment, so the following code is untested... but it should at least give you an indication of what you can do.
public class TripleArrayAdapter extends BaseAdapter {
private String[] array1, array2, array3;
private LayoutInflater inflater;
public TripleArrayAdapter(Context context, String[] a1, String[] a2, String[]a3) {
array1 = a1;
array2 = a2;
array3 = a3;
inflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return array1.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View currentView = convertView;
if(currentView==null) {
currentView = inflater.inflate(R.layout.list_item, parent, false);
}
TextView tView = (TextView)currentView.findViewById(R.id.textView1);
tView.setText(array1[position]);
tView = (TextView)currentView.findViewById(R.id.textView2);
tView.setText(array2[position]);
tView = (TextView)currentView.findViewById(R.id.textView3);
tView.setText(array3[position]);
return currentView;
}
}
Then, to use this adapter as your ListActivity's adapter, you would just say
setListAdapter(new TripleArrayAdapter(this, Array1, Array2, Array3));