listview and notifydatasetchanged in android
I have a CustomListView
with ImageView
.
When I select any item the image view changes. I need to display the selected items in another ListView. I went through documents and I got a clue to use notifydat开发者_JS百科asetchanged()
..but problem is I dont know how to use it or where to use it.
Have a look on the sample Example to know how to use notifyDatasetChanged
Example
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListView extends ListActivity {
ArrayList<String> pens = new ArrayList<String>();
ArrayAdapter arrayAdapter = null;
private static final byte UPDATE_LIST = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pens.add("MONT Blanc");
pens.add("Gucci");
pens.add("Parker");
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, pens);
setListAdapter(arrayAdapter);
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
System.out.println("..Item is clicked..");
Message msg = new Message();
msg.what = UPDATE_LIST;
updateListHandler.sendMessage(msg);
}
});
// System.out.println("....g1..."+PhoneNumberUtils.isGlobalPhoneNumber("+912012185234"));
// System.out.println("....g2..."+PhoneNumberUtils.isGlobalPhoneNumber("120121852f4"));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("...11configuration is changed...");
}
void addMoreDataToList() {
pens.add("item1");
pens.add("item2");
pens.add("item3");
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, id + "You have chosen the pen: " + " " + pen,
Toast.LENGTH_LONG).show();
}
private Handler updateListHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LIST:
addMoreDataToList();
arrayAdapter.notifyDataSetChanged();
break;
}
;
};
};
}
actually notifydatasetchanged
method we are using for adapter
not for listview
.
suppose you are using BaseAdapter for listview.. so use
adaptername.notifydatasetchanged() where your image is changing.
for more info then click here
Put your selected items into an array. And use an adapter to put the items into the listview. How did u do it? And please don't mark your question as a code, it's hard to read now.
lv1 = (ListView) findViewById(R.id.ListView01);
adapter1 = new ArrayAdapter<String>(Osszekapcsolas.this,R.layout.list_black_text,R.id.list_content, yourArray);
lv1.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
where list_black_text in az an xml containing a textview for each rows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/list_content"
android:textColor="#222222"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
/>
</LinearLayout>
But i suppose you know this. However, in this code there is no ImageView. I don't know why it changes, maybe you set the layout_width, layout_height or other parameters wrong in your rows. Hope this helps.
Actually you just tell a adapter listview.
after that in holdar.listview
take a event in that event when image is changes put
adaptername.notifydatasetchanged();
精彩评论