samedi 14 mars 2015

Save list view in android app

I made a custom list with an edit text and add button. The user inputs text into the edit text, hits the add button and it adds the button to a list view. The problem is, once I exit the app all the lists added got deleted. How do I easily save the list view so when I open the app the items added don't get erased.


activity_main:



<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/addItem"
android:hint="Add a new item to List View" />

<Button
android:id="@+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText" >
</ListView>


item_row:



<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://ift.tt/nIICcg"
android:padding="5dip">

<ImageView
android:layout_width="78dip"
android:layout_height="78dip"
android:id="@+id/imgThumbnail"
android:scaleType="centerInside"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ImageView>

<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtTitle"
android:layout_toRightOf="@+id/imgThumbnail"
android:layout_marginTop="6dip"
android:layout_marginLeft="6dip">
</TextView>

<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtSubTitle"
android:layout_toRightOf="@+id/imgThumbnail"
android:layout_below="@+id/txtTitle"
android:layout_marginTop="3dip"
android:layout_marginLeft="6dip">
</TextView>


MainActivity:



public class MainActivity extends ActionBarActivity implements
View.OnClickListener{

EditText et;

Button bt;

Button rbt;

ListView lv;

List<ListViewItem> items;

CustomListViewAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et = (EditText) findViewById(R.id.editText);
bt = (Button) findViewById(R.id.addItem);
bt.setOnClickListener(this);

rbt = (Button) findViewById(R.id.removeItem);
rbt.setOnClickListener(this);

lv = (ListView) findViewById(R.id.listView);
items = new ArrayList<ListViewItem>();
adapter = new CustomListViewAdapter(this, items);

lv.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
// setting onItemLongClickListener and passing the position to the function
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
removeItemFromList(position);

return true;
}
});
}

@Override
public void onClick(View v) {
switch(v.getId()) {

case R.id.addItem:

items.add(new ListViewItem() {{

ThumbnailResource = R.mipmap.ic_launcher;
Title = et.getText().toString();
SubTitle = "Item2 Description";

}});
lv.setAdapter(adapter);
break;
}

}

class ListViewItem {

public int ThumbnailResource;
public String Title;
public String SubTitle;
}

protected void removeItemFromList(int position) {
final int deletePosition = position;

AlertDialog.Builder alert = new AlertDialog.Builder(
MainActivity.this);

alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub

// main code on after clicking yes
items.remove(deletePosition);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();

}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

alert.show();

}
}


CustomListViewAdapter:



public class CustomListViewAdapter extends BaseAdapter {

LayoutInflater inflater;
List<MainActivity.ListViewItem> items;

public CustomListViewAdapter(Activity context, List<MainActivity.ListViewItem> items) {
super();

this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

MainActivity.ListViewItem item = items.get(position);

View vi=convertView;

if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);

ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);

TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);

TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);

imgThumbnail.setImageResource(item.ThumbnailResource);

txtTitle.setText(item.Title);

txtSubTitle.setText(item.SubTitle);

return vi;
}
}

Aucun commentaire:

Enregistrer un commentaire