Button inside SimpleCursorAdapter - android

I've a ListView that I populate with a SimpleCursorAdapter. For each item in my list i've, in this order: TextView > RatingBar > TextView > ImageView. I want to add a button on the ImageView but I dont know how to this...
Where I populate the ListView:
adapter = new SimpleCursorAdapter(this, R.layout.beerdrunk, cursor,
new String[] { "beers", "notes", "numbers" }, new int[] {
R.id.champName, R.id.champNote, R.id.champDrunk });
adapter.setViewBinder(binder);
list = (ListView) findViewById(R.id.listMyBeers);
list.setItemsCanFocus(true);
list.setOnItemClickListener(onClickBeer);
list.setAdapter(adapter);
The OnItemClickListener:
private OnItemClickListener onClickBeer = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println(""+ arg0);
System.out.println(""+ arg1);
System.out.println(""+arg2);
Intent newActivity = new Intent(MyBeers.this, DetailsBeer.class);
newActivity.putExtra("com.example.checkmybeer.DetailsBeer", ""
+ arg3);
startActivityForResult(newActivity, 0);
}
};
And the XML for beerdrunk:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/champName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="130"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:maxLines="3"
android:padding="5dp"
android:paddingRight="20dp"
android:singleLine="false"
android:text="#string/beerBook"
android:textColor="#color/black1"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="1.5dip"
android:layout_height="fill_parent" />
<RatingBar
android:id="#+id/champNote"
style="#style/beerRatingSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="true"
android:numStars="5"
android:layout_margin="1.5dp"
android:rating="5"
android:stepSize="0.10" >
</RatingBar>
<View
android:layout_width="1.5dip"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/champDrunk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="30"
android:gravity="right|center_vertical"
android:padding="5dp"
android:layout_marginLeft="5dp"
android:text="200"
android:textColor="#color/black1"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:layout_width="1.5dip"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/champPlusone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_margin="5dp"
android:layout_weight="30"
android:gravity="right|center_vertical"
android:clickable="false"
android:src="#drawable/plus2" />
</LinearLayout>
I've tried something like:
if(arg2 instanceof ImageView){
//do some stuff
}
But it does not work... How can i do to know which view is clicked inside my "row" ?

If you want to click on an image, use an ImageButton instead of an ImageView.
You can then set the onClick attribute on the button that calls a method in your Activity.
Extend a SimpleCursorAdapter and in the bindView method add onClick listeners for each button. (Maybe make it call a method in your activity ... public void buttonClicked(int position, View v)... so you know which row and which view was clicked.
Here is an example implementation of CursorAdapter you could use.
class CustomCursorAdapter extends CursorAdapter{
LayoutInflater inflater;
public CustomCursorAdapter(Context context, Cursor c, int flags){
super(context,c,flags);
inflater = LayoutInflater.from(context);
}
#Override
public void bindView(View view, Context context, Cursor cursor){
int row_id = cursor.get('_id'); //Your row id (might need to replace)
ImageButton button = (ImageButton) view.findViewById(R.id.champPlusone);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
}
});
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.beerdrunk, parent, false);
bindView(v,context,cursor);
return v;
}
}
;)

Related

How give number to listview items in android

I am new to android . I have an application working with listview and i want to number each list view item.
Like,
1 list item
2 list item
3 list item
MainActivity.java
public class MainActivity extends AppCompatActivity {
private OrderDbManager orderDbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
final String[] from = new String[] { OrderDbHelper.FOOD_NAME,
OrderDbHelper.QUANTITY, OrderDbHelper.PRICE };
final int[] to = new int[] {R.id.tvFoodName, R.id.tvItemQuantity, R.id.tvItemPrice };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orderDbManager = new OrderDbManager(this);
orderDbManager.open();
final Cursor cursor = orderDbManager.fetch();
listView = (ListView) findViewById(R.id.listView);
listView.setEmptyView(findViewById(R.id.empty));
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
adapter.notifyDataSetChanged();
LayoutInflater layoutInflater = getLayoutInflater();
listView.addHeaderView(layoutInflater.inflate(R.layout.cart_header,listView, false));
listView.setAdapter(adapter);
}
public class MyAdapter extends SimpleCursorAdapter {
public MyAdapter(Context context, int layout, Cursor c,
String[] from,int[] to) {
super(context, layout, c, from, to);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null, true);
TextView txt= (TextView) convertView.findViewById(R.id.tvSlNo);
txt.setText(position + 1);
return convertView;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
list_item.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="wrap_content"
android:layout_margin="5dp"
android:minHeight="50dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/tvSlNo"
android:layout_width="0dip"
android:layout_height="match_parent"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:layout_weight="0.1" />
<TextView
android:id="#+id/tvFoodName"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:text=""/>
<TextView
android:id="#+id/tvItemQuantity"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:textStyle="normal"
android:textColor="#android:color/black"
android:gravity="center"/>
<TextView
android:id="#+id/tvItemPrice"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:text=""/>
</LinearLayout>
i want to set numbering in tvSlNo.
please help
In your getView() method of your listViewAdapter simply use the position variable and set it on your TextView. You may need to add 1 to the position since it starts with 0. So use (position+1) and then set it to your TextView
Though this question may be too late but for other reference purposes.
Check where you have this code: txt.setText(position + 1);
you are close to your solution. You just have to typecast position + 1 to String since setText() only take String alone.

Listview inside DialogueFragment unclickable

My app has listview inside dialogueFragment , when user chose item it should Toast , in my case items unclickable .
DialogueFragment
public class HistoryDialogue extends DialogFragment {
ListView list ;
Cursor c ;
HistoryAdapter adapter ;
Context context ;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.history,null);
list = (ListView)view.findViewById(R.id.listView);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
ContentResolver resolver = getActivity().getContentResolver();
c=resolver.query(Contract.SaveRunning.CONTENT_URI,null,null,null,null);
adapter= new HistoryAdapter(getActivity(),c,0);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"keep going ",Toast.LENGTH_LONG).show();
}
});
builder.setView(list);
return builder.create();
}
}
CursorAdapter
public class HistoryAdapter extends CursorAdapter {
public HistoryAdapter(Context context, Cursor c, int flags) {
super(context,c, flags);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.historylist, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text = (TextView) view.findViewById(R.id.text);
String body = cursor.getString(cursor.getColumnIndex("name"));
text.setText(body);
}
}
history.xml
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal" />
historylist.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text"
android:layout_margin="5dp"
android:layout_weight="1"/>
<Button
android:layout_width="30dp"
android:layout_height="35dp"
android:background="#drawable/delete"
android:id="#+id/btn"
android:layout_margin="10dp"
android:layout_weight="0"
/>
Add android:focusable="false" in your ListView (in history.xml ) and Item main layout (in historylist.xml).
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal" />
and
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
...
Add this line to your CursorAdapter's bindView:
((ViewGroup)row).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
if it isn't enough, add clickable="false" to your historylist.xml's Button

How to get the value for each seek bar created in list view Android?

may I know how to get the specific value for each SeekBar which I have created in custom ListView? I am able to create the static one but what if i want to retrieve the value for each SeekBar in the custom ListView based on my sliding? Thank you.
--Activity code--
public class DisplayAssessmentActivity extends ActionBarActivity {
ListDatabaseHandler listCriteria;
private SimpleCursorAdapter dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_assessment);
//Open connection and get instance from database
listCriteria = new ListDatabaseHandler(this);
listCriteria.open();
//Delete all data
listCriteria.deleteAllCriteria();
//Add data
listCriteria.insertSomeCriteria();
//Retrieve data from database and display in list view
displayCriteriaListView();
}
private void displayCriteriaListView() {
Cursor cursor = listCriteria.fetchAllCriteria();
// The desired columns to be bound
String[] columns = new String[] {
ListDatabaseHandler.KEY_CRITERIANAME,
ListDatabaseHandler.KEY_CRITERIAWEIGHTAGE,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.criteriaName,
R.id.criteriaWeightage
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_list_criteria,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.criteriaListView);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//Action when click on the item
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
//Get matric number from this row in the database
String criteria = cursor.getString(cursor.getColumnIndexOrThrow("criteria_name"));
// Get the state's capital from this row in the database.
String weightage =
cursor.getString(cursor.getColumnIndexOrThrow("criteria_weightage"));
//Toast.makeText(getApplicationContext(),
//subjectName, Toast.LENGTH_SHORT).show();
Intent intentResult = new Intent(getApplicationContext(), ResultActivity.class);
startActivity(intentResult);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_assessment, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_list_criteria.xml
enter code here
//Label criteria
<TextView
android:id="#+id/txtViewCriteria"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Criteria : " />
//Label criteria from db
<TextView
android:id="#+id/criteriaName"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:text="Criteria" />
<TextView
android:layout_width="180dp"
android:layout_height="fill_parent" />
</LinearLayout>
//SeekBar layout
<SeekBar
android:id="#+id/sbBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:max="5"
android:paddingTop="20dp" />
//TextViews for SeekBarlegend
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvLabel1"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="#+id/tvLabel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:text="5" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp">
//Label weightage
<TextView
android:id="#+id/txtViewWeightage"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weightage: "/>
//Label weightage from db
<TextView
android:id="#+id/criteriaWeightage"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/stuMatric"
android:layout_toRightOf="#+id/textView2"
android:text="Weightage" />
</LinearLayout>
activity_display_assessment.xml
//Title Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/orangehome"
android:orientation="horizontal">
<TextView
android:layout_width="110dp"
android:layout_height="fill_parent" />
<TextView
android:layout_width="170dp"
android:text="Evaluate"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:layout_height="60dp" />
<TextView
android:layout_width="50dp"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnLogout"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#drawable/logout"/>
</LinearLayout>
</LinearLayout>
//Criteria layout
<TextView
android:id="#+id/txtViewSubject"
android:paddingTop="10dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="60dp"
android:textSize="18sp"
android:textColor="#000000"
android:text="Criteria"/>
//List view for criteria
<ListView
android:id="#+id/criteriaListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
So far, the method below solve my question.
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
/**
* Here we go and get our rowlayout.xml file and set the textview text.
* This happens for every row in your listview.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.criteriaName);
final TextView textView2 = (TextView)rowView.findViewById(R.id.txtView2);
SeekBar seekBar = (SeekBar)rowView.findViewById(R.id.sbBar);
// Setting the text to display
textView.setText(values.get(position));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
textView2.setText(""+i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return rowView;
}
}
Try this:
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
I hope it helps.
Simple and elegant:
yourListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
seekbar = (SeekBar)yourListView.getItemAtPosition(position);
seekbar.setFocusable(true);
seekbar.setEnabled(true);
int value = seekbar.getProgress(); // get value here
}
});
Try this and let me know if it works or not.
This is not Tested, so if you only enter SeekBars into your ListView only then this will work.

How to add footer button in horizontal listview

I want to add an button to the footer of listview, I'm using horizontal listview, The adapter class:
public ItemAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to,0);
this.layout = layout;
inflator= LayoutInflater.from(context);
}
I return the newView and bindView:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflator.inflate(layout, parent, false);
TextView footer = (TextView)v.findViewById(R.id.bAdd);
return v;
}
#Override
public void bindView(View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex(ItemDb.ID));
final String name = c.getString(c.getColumnIndex(ItemDb.NAME));
}
and the activity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { ItemDb.NAME, ItemDb.CONDITION, ItemDb.EMAIL};
int[] to = new int[] { R.id.etItemName, R.id.etItemCondition, R.id.etEmail };
itemAdapter = new ItemAdapter(this,R.layout.list_item, null, from, to);
this.setListAdapter(itemAdapter);
}
How can I add the button in listview footer?
Here is my 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" >
<ImageView
android:id="#+id/photo"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/photo_frame"
android:contentDescription="#string/imagedescription"
android:src="#drawable/ic_launcher" >
</ImageView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_marginLeft="10dp"
android:text="#string/condition"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/condition"
android:layout_marginLeft="10dp"
android:text="#string/email"
android:textColor="#000"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
Override getCount() of listview adapter to return data.size()+1 elements;
In getView() insert something like this:
if (position == data.size()){
return getFooterView();
}
In getFooterView() inflate proper layout with button.

How to change button background when you click on the listview item

I have a listview and I have inflate it with a custom layout containing 1 ImageView, 2 TextView and 1 Button. I want to change the button background when I click on a listview item , but I don't be able to perform it. Can someone help me, please? Thank you so much in advance.
row_segnalibro.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="50dp" >
<ImageView android:id="#+id/ImageView01" android:src="#drawable/star"
android:layout_height="40dp"
android:layout_width="40dp"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout02"
android:orientation="vertical"
android:layout_width="220dp" android:layout_height="match_parent">
<TextView
android:id="#+id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvRow2"
android:layout_below="#id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
</RelativeLayout>
<Button
android:id="#+id/butt_segnalib"
android:background="#drawable/freccia_o"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center" />
Segnalibro.java
public class Segnalibro extends ListActivity{
super.onCreate(savedInstanceState);
setContentView(R.layout.segnalibro);
lv = (ListView) getListView();
.....some code
MySimpleCursorAdap myadap = new MySimpleCursorAdap(getApplicationContext(),R.layout.rowlayout_segnalibro,curr,campi, to);
lv.setAdapter(myadap);
lv.setClickable(true);
lv.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,int position, long arg3) {
Log.i("","I have clicked on item "+position);
}
});
MySimpleCursorAdap.java
public class MySimpleCursorAdap extends SimpleCursorAdapter{
public MySimpleCursorAdap(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mLayout = layout;
this.curr = c;
this.cont = context;
}
public void bindView(View view, Context context, final Cursor cursor) {
....some code
/* Button butt = (Button) view.findViewById(R.id.butt_segnalib);
butt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
.....code to change the background
}
}); */
}
in MySimpleCursorAdap I can get the Button with the method findViewById, how I can to do it in the class Segnalibro?
Ok.. so basically you need a state list for acheiving it.. see this to know how to create it.. and then set it as background of your button..
When you use ListActivity as base class you don't have to create a onItemClickListener yourself, it's already there. Just override protected void onListItemClick(ListView l, View v, int position, long id). Inside you will be able to fetch the Button via it's id.
http://developer.android.com/reference/android/app/ListActivity.html
protected void onListItemClick(ListView l, View v, int position, long id)
{
Button bt = (Button) v.findViewById(R.id.button);
//do something fancy with the button
}
Try to add a ClickListener for the list item view in the bindView method:
public void bindView(View view, Context context, final Cursor cursor) {
//some code
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final ImageButton button = (ImageButton)v.findViewById(R.id.list_item_button);
button.setBackgroundColor(...)
}
});
)

Categories

Resources