How to add/remove item from listview in android when click button in item listview
Date : March 29 2020, 07:55 AM
Does that help I'm a newbie android.I have some problem about my mini app. You can see figure below: http://i481.photobucket.com/albums/rr175/viethungit/android.pngpublic class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
private ImageView myImage;
public static String upload= " ";
public static String GalleryImage;
public ArrayList<ListItem> myItems = new ArrayList<ListItem>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listandimage);
myList = (ListView) findViewById(R.id.MyList);
myImage= (ImageView)findViewById(R.id.image1);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
ListItem listItem = new ListItem();
listItem.textdata="@";
listItem.caption = "";
myItems.add(listItem);
myList.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return myItems.size();
}
public ListItem getItem(int position) {
return myItems.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.text=(TextView )convertView.findViewById(R.id.textView1);
holder.captionEditText = (EditText) convertView.findViewById(R.id.ItemCaption);
holder.addOrDeleteButton = (Button) convertView.findViewById(R.id.buttonAdd);
holder.captionEditText.setFocusable(true);
holder.captionEditText.requestFocus();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill EditText with the value you have in data source
// holder.captionEditText.setId(position);
holder.text.setTag(position);
holder.captionEditText.setTag(position);
holder.captionEditText.setText(getItem(position).caption);
holder.addOrDeleteButton.setTag(position);
// / this updates tag of
// the button view as we
// scroll ///
holder.addOrDeleteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
int tag = (Integer) view.getTag();
if (tag != (myItems.size() - 1)) {
myItems.remove(tag);
Log.d("GCM", "Item removed from " + tag);
myAdapter.notifyDataSetChanged();
} else {
ListItem listItem = new ListItem();
listItem.textdata="@";
listItem.caption = "";
myItems.add(listItem);
/*
* Log.d("GCM", holder.captionEditText.getText()
* .toString()); myItems.get((Integer)
* view.getTag()).caption = holder.captionEditText
* .getText().toString();
*/
myAdapter.notifyDataSetChanged();
myList.setSelection(myAdapter.getCount() - 1);
// holder.captionEditText.setFocusable(true);
// holder.captionEditText.requestFocus();
}
}
});
holder.captionEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// if(position < myItems.size())
// getItem(position).caption = s.toString();
myItems.get((Integer) holder.captionEditText.getTag()).caption = holder.captionEditText
.getText().toString();
}
});
if (position != (myItems.size() - 1)) {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttarecloseicon);
} else {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttareaddicon);
holder.text.setFocusable(true);
holder.captionEditText.setFocusable(true);
holder.text.requestFocus();
holder.captionEditText.requestFocus();
}
return convertView;
}
}
class ViewHolder {
TextView text;
EditText captionEditText;
Button addOrDeleteButton;
}
class ListItem {
String textdata;
String caption;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView1"
android:layout_width="15dp"
android:layout_height="50dp"
android:text=" @"
/>
<EditText
android:id="@+id/ItemCaption"
android:layout_width="270dp"
android:layout_height="50dp"
android:layout_margin="3dip"
android:imeOptions="actionDone|flagNoExtractUi"
android:inputType="textNoSuggestions"
android:singleLine="true" >
</EditText>
<Button
android:id="@+id/buttonAdd"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_margin="3dip"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="horizontal" >
<ListView
android:id="@+id/MyList"
android:layout_width="370dp"
android:layout_height="160dp"
android:layout_gravity="center|top"
android:layout_marginLeft="150dp"
android:descendantFocusability="beforeDescendants"
>
</ListView>
<ImageView
android:id="@+id/image1"
android:layout_width="80dp"
android:layout_height="80dp"
/>
</LinearLayout>
|
ListView OnLongClick strange behaviour
Tag : java , By : user160048
Date : March 29 2020, 07:55 AM
I hope this helps . ListViews recycle Views, so you only have a few views for all of your items. You're directly changing one of these view instances to switch between the info|brief. What you need is to save the status of the info|brief flag for the affected position somewhere else (e.g. a list of positions that should be "briefs" in the adapter). That way when you come back into getView() you can display the right one.
|
How to use onLongClick method in ListView?
Tag : java , By : AJacques
Date : March 29 2020, 07:55 AM
will be helpful for those in need Hello guys I'am totally new in android and I have an assignment that will output an option when a user long clicks the item that is in list view. Can you help me about this? thanks in advance. I get this code in the internet and try to tweak it but I don't know where to put the method onlongclick. please help me guys , you havent initialize your listview with xml listview.. ListView lst=null;
lst = (ListView) findViewById(R.id.xmllistviewid);
|
How to change ListView Item's background when onLongClick if adapter extends CursorAdapter
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have ListView which gets data from SQLite and a Cursor Adapter. , You should do something like if (mActionMode != null)
{
view.setBackgroundColor(Color.TRANSPARENT);
mActionMode.finish();
return false;
}else
{
mActionMode = Tctivity.this.startActionMode(TActivity.this);
view.setSelected(true);
view.setBackgroundColor(Color.parseColor("#b8dbd3"));
}
|
Listview onlongclick delete
Date : March 29 2020, 07:55 AM
this will help As you are deleting data from your database but the list attached to adapter still holds the data, you have to remove from that position and notify your adapter. Try this code: listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
myDB.deleteName(theList.get(i));
theList.remove(i);
listAdapter.notifyDataSetChanged();
Toast.makeText(ListDataActivity.this, "item removed", Toast.LENGTH_SHORT).show();
return true;
}
});
public void deleteName(String name){
SQLiteDatabase db=this.getWritableDatabase();
String query= "DELETE FROM "+ TABLE_NAME +" WHERE " + COL2 + "= '" + name + "'";
db.execSQL(query);
}
myDB.deleteName(theList.get(i));
|