I am making a ListView with TextView and 1 delete button for each row.
To populate the list I am using my custom adaptor (extends base adapter) and sqlite db to map into list.
My requirement is onclick of delete button in a row that record should be deleted and list should refresh.
I am able to delete record from db but my list is not refreshing until I rotate the device or assign a new instance of my adapter from activity.
I have tried following answer
but didn't work in my case. the difference between this answer and my case is I am using baseAdapter and he is using cursorAdapter.
public class BookmarksPDFAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
openDatabase();
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
deleteBookmark(getLocation(v));//getlocation(View) method returns which delete button clicked
notifyDataSetChanged();
}
});
}
closeDatabase();
return convertView;
}
my activity looks like
public class BookmarkActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
btnEdit = (Button) findViewById(R.id.edit_bookmarks);
btnAdd = (Button) findViewById(R.id.add_bookmarks);
list = (ListView) findViewById(android.R.id.list);
adapter = new BookmarksPDFAdapter(this);
list.setAdapter(adapter);
}
bookmark.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10.0"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:weightSum="1.0"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/iconShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/delete_icon"
android:visibility="invisible"
android:layout_weight="1.0"/>
</LinearLayout>
<TextView
android:id="#+id/bookmark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="7.0"
android:gravity="center_horizontal|center_horizontal"
android:lines="1"
android:text="#+id/TextView01"
android:textSize="24dp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2.0"
android:text="#string/btn_txt_delete"
android:visibility="invisible" >
</Button>
listitem.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10.0"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:weightSum="1.0"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/iconShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/delete_icon"
android:visibility="invisible"
android:layout_weight="1.0"/>
</LinearLayout>
<TextView
android:id="#+id/bookmark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="7.0"
android:gravity="center_horizontal|center_horizontal"
android:lines="1"
android:text="#+id/TextView01"
android:textSize="24dp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2.0"
android:text="#string/btn_txt_delete"
android:visibility="invisible" >
</Button>
deleteBookmark method
void deleteBookmark(int wantedChild) {
String bookmarkItem = getBookmarkItemText(wantedChild, true);
datasource.open();
int check = datasource.deleteBookmark(bookmarkItem);
if (check == 1) {
btnDelete = (Button) (viewList.get(wantedChild)
.findViewById(R.id.btnDelete));
btnDelete.setText(R.string.btn_txt_deleted);
btnDelete.setEnabled(false);
}
datasource.close();
}
Here I am deleting record from my database and changing text of delete button from delete to deleted
adapter.notifyDataSetChanged();
You can call the above method to refresh list view any time. In your case call it after you delete a record from database.
I update listview by calling:
listview.invalidateViews();
notifyDataSetChanged();
is the only solution for me also now my deleteBookmark method looks like
void deleteBookmark(int wantedChild, String bookmarkItem) {
openDatabase();
int check = datasource.deleteBookmark(bookmarkItem);
if(check==1)
Toast.makeText(context, R.string.msg_bookmark_delete, ReaderConstants.TOAST_SHOWTIME).show();
else
Toast.makeText(context, R.string.msg_bookmark_delete_failed, ReaderConstants.TOAST_SHOWTIME).show();
notifyDataSetChanged();
closeDatabase();
}
inside adapter set the custome method for update dataset and notify changes
public void updateData(ArrayList<DataSet> d) {
this.data = d;
notifyDataSetChanged();
}
Then call method inside Activity,
(for my case ,I am calling inside onResume Method)
#Override
public void onResume(){
super.onResume();
adapter.updateData(dataList);
}
Related
I want to add filter button to my onCreateOptionsMenu. When button was clicked i need to show layout with checkboxes and apply button.
Only way that i know how to do this - start new activity from onOptionsItemSelected and do all operations there. But is there more efficient way? Without leaving current activity. Something like pop-up window etc.
You can do it By Using this Material Dialog
https://github.com/drakeet/MaterialDialog Library and Below code, Just
put the gravity of Dialog TOP|RIGHT.
private void showMaterialDialog() {
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice);
for (Card card : cards) {
arrayAdapter.add(card.getName());
}
alert = new MaterialDialog(this);
View mView = getLayoutInflater().inflate(R.layout.custom_view_virtual_card, null);
ListView listView = (ListView) mView.findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
AppCompatButton btnCancel = (AppCompatButton) mView.findViewById(R.id.btnCancel);
AppCompatButton btnOk = (AppCompatButton) mView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAsyncForRequestVirtualCard(selectedCardTypeId);
alert.dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCardTypeId = cards.get(position).getId();
Log.d("request", cards.get(position).getId() + " " + cards.get(position).getName());
}
});
alert.setView(mView);
alert.show();
}
custom_view_virtual.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Select Card Type"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/colorAccent" />
<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:background="#color/divider" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:divider="#color/transparent"
android:theme="#style/ProgressBarStyle"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnCancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="#string/cancel"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnOk"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Don't create a new activity instead create a new java class in that apply your sorting logic to filter the content and when your button is pressed that activity should return all your sorted content using List class or ArrayList class or anything else then display it.
I'am trying to click a button on my list view to update that row first of all I tried to click on the item in the list view to show toast.
I did setOnItemClickListener of the ListView
public class Medlistview extends Activity{
DB db = new DB(this);
ImageButton updateimagebutton = (ImageButton)findViewById(R.id.editbuttonimage);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medlistview);
populatemedlistview();
}
public void populatemedlistview(){
Cursor cursor = db.getMEDINF();
String[] medinfo = new String[]{
DB.col1,DB.col2,DB.col3,DB.col4,DB.rowID
};
int[] mappingmed = new int[]{
R.id.MEDid,R.id.MedName,R.id.Medpurpose,R.id.medNotaplet,R.id.ROWID
};
SimpleCursorAdapter medcursoradapter = new SimpleCursorAdapter(this,R.layout.medlistviewxml,cursor,medinfo,mappingmed);
ListView medlistview = (ListView)findViewById(R.id.listViewmed);
medlistview.setAdapter(medcursoradapter);
medlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Medlistview.this,"clicked",Toast.LENGTH_LONG).show();
}
});
}
}
I made sure that my list view is clickable and focusable but still not working
One more thing I already define image button on layout item not on ListVew layout every time I run the app, is crashing and showing that null reference message in the log-cat do I need to define the button also on the ListView layout.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
My xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="282dp"
android:id="#+id/listViewmed"
android:layout_weight="0.40"
android:background="#c7c0c0"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
Here is my list item xml layout file.
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/MEDid"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/MedName"
android:layout_below="#+id/MEDid"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/Medpurpose"
android:layout_below="#+id/MedName"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/medNotaplet"
android:layout_below="#+id/Medpurpose"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editbuttonimage"
android:layout_above="#+id/Medpurpose"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/editicon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/ROWID"
android:layout_alignBottom="#+id/medNotaplet"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Thanks in Advance.
Implement View.OnClickListener() to your ImageButton in getView() method in your adapter, not in Medlistview activity. And use callback or eventBus to work with click action in your activity.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.recycler_view_item, parent, false);
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Button was clicked
}
});
return convertView;
}
Set
android:focusable="false"
android:focusableInTouchMode="false"
for all UIs controls in your custom layout file.
use android:descendantFocusability="blocksDescendants" inside listview,
check out this answere: can't click on listview row with imagebutton
Set the parent layout of the item
android:descendantFocusability="blocksDescendants"
and set
android:clickable="false"
android:focusable="false">
on all your widgets.
I want to connect imagebuttons to create a word on the textview. How can I do this?
The idea of using OnTouchListener is that i think its more "accurate" in my app. Heres the sample image of my app.
Please help me solve this problem. I am new in android programming. I saw some codes using Motion Event and has cases ACTION_DOWN and ACTION_UP but I cant understand how it is used.
Any help with codes will be highly appreciated.
here's my code:
public class MainActivity extends Activity {
//VARIABLES HERE
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//R.ID.XML ..
//randomize image display
// DISPLAY TEXT IN TV from IMGBTN
OnClickListener myCommoClickListner = new OnClickListener(){
#Override
public void onClick(View arg0) {
Log.i(TAG,"arg0.getId() = " + arg0.getId());
if(arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+"a"; //[PLACEE RESPACTIVE CHARACTEER HERE]
text.setText(generatedString);
((ImageButton) arg0).setImageResource(R.drawable.changea);
if (!timeHasStarted) {
countDownTimer.start();
timeHasStarted = true;
}
}
}
};
//CUSTOMCLICKLISTENER
}
//COUNTDOWNTIMER
// SEARCH WORD, ADD WORD TO LV (if in database), SCORE
}
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"
android:orientation="vertical"
android:background="#drawable/bg" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" >
<ImageButton
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" >
<ImageButton
android:id="#+id/Button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" >
<ImageButton
android:id="#+id/Button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp" >
<ImageButton
android:id="#+id/Button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
<ImageButton
android:id="#+id/Button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello_world" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
i will give you an idea because you made it clear you have no idea, so writing codes will make no sense, that's why you couldnt provide codes yourself, so Sir, the Imagebutton from what i see with this image is in a viewgroup, now go this documentaion to learn about viewgroups then get your touch listener or event listener, and focus it on your view group, and get the particular child -( Imagebutton) with forcus, in your view group with Viewgroup.findFocus() and then move on.. Get the idea now?? try it..
EDIT1:-= First all, great and i get you..but why dont you use gridview and make them act like they buttons, and with that you can have or be able to connet them by catching the item that was clicked and doing what you wona do..
so basically you add the letters to the items(buttons) in your adapter and then get the clicked item in your girdview here is just some intro in your adapterinflate or create a button and add the letter to it by View.setTag((String)"K-your letter") then in your onclick get it by
GridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, final View arg1, final int arg2,long arg3) {
//gettting your letters
String letter = (String) arg1.getTag();
//add it to your word formation using any custom method.
}
});
this is much easier looking at what you want...plus you could style the getview..
Real Deal (Me tryna use your codes)
now coming to your way of it..declaring each imagebutton in xml, will, be a lot tedious..
so do this, in your java
for(int i = 0; i < 16){
ImageButton ib = new ImageButton(Context);
ib.setClickable(true);
ib.setFocusableInTouchMode(true);
ib.setFocucsable(true);
ib.setTag((String)"K-your letter") // i am using tags, but save/add it they way you save it
ViewGroup.addView(ib); // create a viewgroup object and add it to it, dont use the class like i did.. but should be like this
}
// then your viewgroup as an object setOnclick listener for it..
ViewGroup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
View myclickedimagebutton = ViewGroup.findFocus(); // this finds the view that has focus in the viewgroup, so the view that was clicked
// now flirt with myclickedimagebutton
//when you done flirting with him
ViewGroup.clearChildFocus(myclickedimagebutton); // making sure he now loosees focus
}
});
NOTE my closing tags may be mal formed check it and try it, im writing from a raw hand
also my code formats may also be wrong so check for silly spellings..
also when you create your viewgroup, set focusable, clickable to true..
let me know if it helps
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I am using gridview
GridView gv = (GridView) findViewById(R.id.gridView1);
//this is the adapter for your 16 buttons..
Adapter adapt; // we have not instatiate it yet..
//this is your items to fill yyour adapter ,that is, your random letters
ArrayList<String> letters = new ArrayList<String>(); //add your letters to it..
//using the logic on one of my answers on your questions add 16 different letter string to this letters array.
//i dont think i am pose to cover that right?
//instatiate adapt here below
adapt = new Adapter(context, R.layout.layout_containing_your_imagebutton, letters);
gv.setAdapter(adapt); //setting the adapter
gv.setOnTouchListener(new OnTouchListener() {// this in the ontouch
#Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
// this is when the user's hand is on the gridview
//docode can comes here
return true;
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
// this is when the user's hand is moving on the gridview
//docode can comes here
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP){
// this is when the user's hand is raised up;;
return true;
}
return false;
}
});
}
void dococde(ViewGroup v){ // the v here is the arg0 in the touch listener- dont forget to cast it when you reference it
//put docode in one of the touch event that pleases you..
String clicked_or_touch_letter_for_formation_by_user = (String) v.getFocusedChild().getTag();
// add clicked_or_touch_letter_for_formation_by_user to your word adapter.
}
static class Adapter extends ArrayAdapter<String> {
ArrayList<String> list;
Context context;
int resource;
public Adapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
this.list = objects;
this.context = context;
this.resource = resource;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View child = convertView;
RecordHolder holder;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (child == null) {
child = inflater.inflate(resource, parent, false);//resource should be one layout with one imagebutton..
// that's the imagebutton you want to inflate 16 times.. or have in your gridview..
holder = new RecordHolder();
holder.letter = (ImageButton) child.findViewById(R.id.yourimagebuttonid); //id to your imagebutton
child.setTag(holder);
}else{
holder = (RecordHolder) child.getTag();
}
final String iLetter = list.get(position);
holder.letter.setTag(iLetter);
return child;
}
}
static class RecordHolder {
ImageButton letter;
}
i got a lil busy.. but is just the idea or logic im talking about, try and it, note my closing tags and other formatting might be bad or mal formed so correct them with the edittors and add your respective id's and layouts and try, will work.. and dont forget to upvote or notify if it helped you
I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event
As of now I can access its position as shown below
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//btnNxt.setEnabled(true);
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Now I would like to know how do I access the selected position outside(ie..on button click event).
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here I need to get the position and selected item
}
});
Listview.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
ItemDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/sampleimg"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/itemname"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/carname"
android:focusable="false"/>
<TextView android:id="#+id/itemprice"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cardesc"/>
<com.example.abc.widget.InertCheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:button="#drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>
OrElse how do I check If any listview item is checked or not on button click ?
EDIT:
This is the way I'm trying to access the position from button click
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
myposition,
Toast.LENGTH_SHORT).show();
}
});
Simply make a member variable (declare it outside of a method like before onCreate() and use that.
public class ...
{
int pos;
// onCreate() and such
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
pos = position; // initialize it
...
}
});
then use pos inside your onClick() or wherever you need to in the Activity.
If I completely missed something in your question then please explain.
I have used a Custom ListView and I am displaying some data using the same ListView. When I click on the List View item, the onClickListener is not getting called. I am not able to select any list item.
Layout Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dp"
android:background="#drawable/list_selector"
android:clickable="true"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="#+id/imgProperty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher"
android:focusable="false"/>
</LinearLayout>
<TextView
android:id="#+id/tvCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="75dip"
android:layout_toRightOf="#+id/list_image"
android:paddingBottom="10dip"
android:text="property"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/tvprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgProperty"
android:layout_alignLeft="#+id/tvCity"
android:text="Price"
android:focusable="false"/>
</RelativeLayout>
Adapter code:
public class CustomListAdapter extends BaseAdapter {
ArrayList<Propety> PropertiesArray;
private LayoutInflater Inflater;
public CustomListAdapter(ArrayList<Propety> PropertiesArray) {
this.PropertiesArray=PropertiesArray;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PropertiesArray.size();
}
#Override
public Object getItem(int position) {
return PropertiesArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.customlistview, parent, false);
}
final Propety ListArray = PropertiesArray.get(position);
TextView tvPropertyName = (TextView) convertView.findViewById(R.id.tvCity);
tvPropertyName.setText(ListArray.getName());
TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice);
tvPrice.setText(ListArray.getPrice());
ImageView imgProperty = (ImageView) convertView.findViewById(R.id.list_image);
imgProperty.setImageResource(R.drawable.ic_launcher);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
ListView code:
ListView propertylistview = (ListView) findViewById(R.id.listview);
CustomListAdapter customlistview=new CustomListAdapter(PropertiesArray);
propertylistview.setAdapter(customlistview);
ListView XML:
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/customview"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="24dp"
android:background="#drawable/list_selector"
android:textAlignment="center" >
custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/txtangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="115dp"
android:layout_marginLeft="95dp"
android:text="" />
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/customview"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="24dp"
android:background="#drawable/list_selector"
android:textAlignment="center" >
</ListView>
<view
android:id="#+id/customview"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.example.samplebuapp.CustomCompass" />
<view
android:id="#+id/view1"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_above="#+id/listview"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
class="com.example.samplebuapp.CustomView" />
<LinearLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/listview"
android:orientation="vertical"
android:focusable="false">
</LinearLayout>
</RelativeLayout>
Even scrolling is not working.
I am unable to figure out why is this happening? Am I missing out something?
Any help in resolving this is appreciated.
The following will do the job in your case.
ListView propertylistview = (ListView) findViewById(R.id.listview);
propertylistview.setOnItemClickListener( myListViewClicked ):
OnItemClickListener myListViewClicked = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(YourActivity.this, "Clicked at positon = " + position, Toast.LENGTH_SHORT).show();
}
};
Dont forget to remove the following from the CustomAdapter
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
}
});
If the touch events are getting intercepted inside the cell layout, in the layout for your custom cell, set
android:descendantFocusability="blocksDescendants" on the top level layout of your cell. For me, it was too much of a pain to add xml attributes to every individual view.
Note that this can also be set in code:
cell.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
I also tried this with a ListView inside a cell, and I had to override onTouchEvent to get it to work:
public class NoTouchListView extends ListView {
#Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
the simple code that solved my problem, and method view.setOnClickListener be within my custom adapter
view.setFocusable(false)
Check your list row layout once :
layout or any view should not be `android:clickable="true" and
android:focusable="true"
if it is there just delete and run the application again.
Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,
Added the following properties to the checkbox,
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and ItemClickListner started working.
For detailed example you can go through the link,
http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/
Hope it helps Cheers!!
For TextView you should also use
android:textIsSelectable="false"
https://developer.android.com/reference/android/widget/TextView.html#setTextIsSelectable