Android listview update item

In the previous post, you learnt how to append or add new items to a ListView. In this tip, I am going to show you how to update an item of the ListView. Updating an item of the ListView is simple as adding an item to the ListView. You need to you a ArrayList to store items of the ListView. The ArrayList has set(int index, Object newItem) method to update an item at the specified index parameter. Do not forget to call the notifyDataChanged on the adapter object to refresh the ListView.



Now to have an example app on updating item of a ListView, in the activity_main.xml file, you add a ListView.


<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
>
<ListView
  android:id="@+id/listv"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
</ListView>
</LinearLayout>

In the res/layout directory, you create list_item.xml file. It is used as a row template of the ListView.

<?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">
<TextView
   android:id="@+id/txtitem"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
</LinearLayout>

You create another layout file and save it as input_box.xml file. It defines the layout of an input dialog. The input dialog is displayed when you click an item of the ListView to update.


<?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">

<TextView
android:id="@+id/txtmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<EditText
  android:id="@+id/txtinput"
  android:layout_width="100dp"
  android:layout_height="wrap_content"
  android:layout_weight="2"
/>
<Button
   android:id="@+id/btdone"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Done"
   android:layout_weight="1"
/>

</LinearLayout>
</LinearLayout>
In the MainActivity class, you write code to add initial items to the ListView, and display an input dialog to update the selected  item of the ListView.




import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends ActionBarActivity {
  ArrayList<String> arrayList;
  ArrayAdapter<String> arrayAdapter;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView=(ListView)findViewById(R.id.listv);
    String[] items={"Apple","Banana","Coconut","Grape","Peach","Pear"};
    arrayList=new ArrayList<>(Arrays.asList(items));
    arrayAdapter=new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtitem,arrayList);
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   // Show input box
   showInputBox(arrayList.get(position),position);
}
});

}
  public void showInputBox(String oldItem, final int index){
    final Dialog dialog=new Dialog(MainActivity.this);
    dialog.setTitle("Input Box");
    dialog.setContentView(R.layout.input_box);
    TextView txtMessage=(TextView)dialog.findViewById(R.id.txtmessage);
    txtMessage.setText("Update item");
    txtMessage.setTextColor(Color.parseColor("#ff2222"));
    final EditText editText=(EditText)dialog.findViewById(R.id.txtinput);
    editText.setText(oldItem);
    Button bt=(Button)dialog.findViewById(R.id.btdone);
    bt.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    arrayList.set(index,editText.getText().toString());
    arrayAdapter.notifyDataSetChanged();
    dialog.dismiss();
  }
  });
  dialog.show();
}

}


listview update item

Posted by: Dara | post date: 05-16-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.