Here is my custom List view layout.
I've added an "empty list" text view with id "android:empty", but the 'empty text' view is not displayed when the list is empty.
Any advice please.
UPDATE - updated the layout
<?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="?android:attr/listPreferredItemHeight"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:paddingTop="15dip" android:paddingBottom="15dip"
android:cacheColorHint="#color/match_bg2" android:background="#color/match_bg">
<ImageView android:id="#+id/flag_left" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/flag"
android:src="#drawable/flag_default" />
<LinearLayout android:orientation="vertical"
android:padding="10dip" android:layout_width="0dip" android:gravity="center"
android:layout_weight="1" android:layout_height="fill_parent">
<TextView android:id="#+id/item_match_label"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:gravity="center"
android:textColor="#color/match_fg" android:textStyle="bold"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
/>
</LinearLayout>
<ImageView android:id="#+id/flag_right" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/flag"
android:src="#drawable/flag_default" />
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
Here is my Listview Activity
public class LatestMatchesListActivity extends ListActivity {
...
private void createMatchList() {
/*
Log.i(MY_DEBUG_TAG, "Checking Match store size before passing it to custom adapter: "+mStore.size());
if(mStore.size() == 0){
Log.i(MY_DEBUG_TAG, "Got Empty match store, so checking connectivity..");
Match m = new Match();
if(isOnline()){
Log.i(MY_DEBUG_TAG, "Internet is accessible, so adding no matches object: ");
m.setId(-1);
} else{
Log.i(MY_DEBUG_TAG, "No Internet accessible, so adding mo caonnectivity match object: ");
m.setId(-2);
}
mStore.add(m);
} else {
Log.e(MY_DEBUG_TAG, "Match store is not empty ");
}
*/
//mStore.clear();
Log.i(MY_DEBUG_TAG, "Passing match list to custom adapter: "+mStore.toString());
this.mAdapter = new MatchAdapter(this, R.layout.list_matches, mStore);
this.setListAdapter(this.mAdapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setBackgroundDrawable(null);
lv.setBackgroundResource(0);
}
and the custom ArrayAdapter
public class MatchAdapter extends ArrayAdapter<Match> {
private ArrayList<Match> items;
private final String MY_DEBUG_TAG = "MatchAdapter";
public MatchAdapter(Context context, int textViewResourceId, ArrayList<Match> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_matches, null);
}
Match m = items.get(position);
if (m != null) {
TextView itemLabel = (TextView) v.findViewById(R.id.item_match_label);
ImageView imageFlagLeft = (ImageView) v.findViewById(R.id.flag_left);
ImageView imageFlagRight = (ImageView) v.findViewById(R.id.flag_right);
if(m.getId() == -1){
itemLabel.setText("No Live Matches.");
imageFlagLeft.setVisibility(View.INVISIBLE);
imageFlagRight.setVisibility(View.INVISIBLE);
} else if(m.getId() == -2){
itemLabel.setText("No Intenet connectivity.");
imageFlagLeft.setVisibility(View.INVISIBLE);
imageFlagRight.setVisibility(View.INVISIBLE);
} else {
itemLabel.setText(m.getTeam1() + " v/s " + m.getTeam2());
imageFlagLeft.setImageResource(getFlagResource(m.getTeam1()));
imageFlagRight.setImageResource(getFlagResource(m.getTeam2()));
}
}
return v;
}
private int getFlagResource(String teamName) {
Log.i(MY_DEBUG_TAG, "Getting the flag of " + teamName);
String flagString = "flag_" + teamName.replace(" ", "_").toLowerCase();
int resId = getContext().getResources().getIdentifier(flagString, "drawable", "com.itflux.DroidChirp");
if (resId != 0) {
return resId;
}
return R.drawable.flag_default;
}
}
You have your ListView fit the screen, and in LinearLayout your TextView is displayed under that ListView so it's not visible (it would be, if you'd use a ScrollView).
You need to use as the root element of your layout a RelativeLayout for instance, to have the TextView on top of the ListView.
Seems like the question is still unsolved. So here is how I solved it. I triggered a function which will basically refresh the list layout whenever the data changes as below:
ListView lv = getListView();
lv.requestLayout();
if(aa.arrayValue.size() > 0) {
lv.setVisibility(ListView.VISIBLE);
setListAdapter(new Adaptor(this, R.layout.main_list_item, aa.arrayValue));
}
else {
lv.setVisibility(ListView.INVISIBLE);
TextView tv = (TextView) findViewById(android.R.id.empty);
tv.requestLayout();
tv.setVisibility(TextView.VISIBLE);
}
Hope this helps you out!
Use #+id in empty and #id in list : android:id="#+id/android:empty"
Your problem is with the ListView height property. Set it to wrap_content.
Related
My xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/address" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:layout_below="#+id/address"
android:id="#+id/win_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/win_list"
android:layout_below="#+id/win_title"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/lose_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lose_list"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/cannot_compare_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cannot_compare_list"
android:layout_weight="1" />
</LinearLayout>
And this is my custom Adapter:
public class AnalysisResultAdapter extends ArrayAdapter<ComparisonResult> {
static class ViewHolder {
TextView address;
TextView win_title;
ListView win_list;
TextView lose_title;
ListView lose_list;
TextView cannot_compare_title;
ListView cannot_compare_list;
}
private Hashtable<String, SevenEleven[]> sevenElevenData;
public AnalysisResultAdapter(Context context, int resource, List<ComparisonResult> houses) {
super(context, resource, houses);
}
public void setSevenElevenData(Hashtable<String, SevenEleven[]> sevenElevenData) {
this.sevenElevenData = sevenElevenData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
view = viewInflater.inflate(R.layout.analysis_result_list_item, null);
holder = new ViewHolder();
holder.address = (TextView) view.findViewById(R.id.address);
holder.win_title = (TextView) view.findViewById(R.id.win_title);
holder.win_list = (ListView) view.findViewById(R.id.win_list);
holder.lose_title = (TextView) view.findViewById(R.id.lose_title);
holder.lose_list = (ListView) view.findViewById(R.id.lose_list);
holder.cannot_compare_title = (TextView) view.findViewById(R.id.cannot_compare_title);
holder.cannot_compare_list = (ListView) view.findViewById(R.id.cannot_compare_list);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (this.sevenElevenData.size() == 0) {
return view;
}
ComparisonResult result = getItem(position);
holder.address.setText(result.house.Address);
if (result.win.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.win) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.win_title.setText("win");
holder.win_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.lose.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.lose) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.lose_title.setText("lose");
holder.lose_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.cannotBeCompared.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.cannotBeCompared) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.cannot_compare_title.setText("cannotBeCompared");
holder.cannot_compare_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
return view;
}
}
The problem is: Only the #+id/address TextView shows the text successfully, other things are not shown. But I don't know why this happened.
How can I solve this problem ? Can someone help me ?
Thanks.
Use ScrollView intead of outer(parent) ListView.
And use LisView for the inner ListView.
It gives you the scrolling feature.
1/ layout_below only works in RelativeLayout, you are using a LinearLayout with horizontal orientation so all the elements will be rendered side by side, and all elements have the layout_width:"match_parent" so only the first element will be shown.
2/ when you use a ListView in another scrollView the renderer can't calculate the height of the second one
Like the title say, i create list view which each item display image, three line text, and a button, but the view is so lagging even in samsung galaxy mega dual core, here my item view xml
<RelativeLayout
android:gravity="center_vertical"
android:padding="5.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/compositeListViewItemThumbnailImage"
android:layout_width="60.0dip"
android:layout_height="60.0dip"
android:src="#drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:contentDescription="#string/_empty_strings" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/compositeListViewItemFirstLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/compositeListViewItemThumbnailImage"
android:layout_alignTop="#+id/compositeListViewItemThumbnailImage"
android:layout_alignBottom="#+id/compositeListViewItemThumbnailImage"
android:layout_alignParentRight="true">
<TextView
android:textSize="16.0sp"
android:gravity="center_vertical"
android:id="#+id/compositeListViewItemFirstLineText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:lines="1"
android:inputType="textNoSuggestions" />
<TextView
android:textSize="12.0sp"
android:id="#+id/compositeListViewItemSecondLineText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:lines="1"
android:inputType="textNoSuggestions" />
<TextView
android:textSize="14.0sp"
android:singleLine="true"
android:ellipsize="marquee"
android:lines="1"
android:layout_gravity="center_vertical"
android:id="#+id/compositeListViewItemThirdLineText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:inputType="textNoSuggestions" />
</LinearLayout>
<Button
android:id="#+id/compositeListViewItemActionButton"
android:paddingLeft="5.0dip"
android:paddingTop="5.0dip"
android:paddingRight="5.0dip"
android:paddingBottom="5.0dip"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="30.0dip"
android:layout_marginTop="30.0dip"
android:layout_marginRight="0.0dip"
android:text="#string/hello_world"
android:layout_alignParentRight="true"
style="?android:attr/buttonStyleSmall" />
</RelativeLayout>
as it might / might not be affected by the adapter view, i also put it here
public class SimpleAdapter extends ArrayAdapter implements Serializable {
private ArrayList<?> items;
private int itemLayout;
private String[] itemFieldsName;
private int[] itemElements;
private Context ctx;
public SimpleAdapter(Context ctx, ArrayList<?> items, int itemLayout, int[] itemElements, String[] itemFieldsName) {
super(ctx, itemLayout, items);
this.items = items;
this.itemElements = itemElements;
this.itemLayout = itemLayout;
this.itemFieldsName = itemFieldsName;
this.ctx = ctx;
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(this.itemLayout, null);
}
Object item = this.items.get(position);
for (int i = 0; i < this.itemElements.length; i++) {
Object value = getValue(item, Character.toUpperCase(this.itemFieldsName[i].charAt(0)) + this.itemFieldsName[i].substring(1));
View elementView = null;
if (itemView != null) {
elementView = itemView.findViewById(this.itemElements[i]);
if ((elementView instanceof TextView)) {
((TextView) elementView).setText(value.toString());
} else if ((elementView instanceof SmartImageView)) {
((SmartImageView) elementView).setImageUrl(value.toString());
} else if ((elementView instanceof Button)) {
((Button) elementView).setText(value.toString());
}
}
}
return itemView;
}
private Object getValue(Object obj, String fieldName) {
ArrayList<String> fieldsName = new ArrayList<String>();
int length;
String str = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
Object value = new Object();
try {
value = obj.getClass().getMethod("get" + str, new Class[0]).invoke(obj);
} catch (Exception ex) {
value = fieldName;
}
return value;
}
private String trimString(String string, int length, boolean soft) {
if(string == null || string.trim().isEmpty()){
return string;
}
StringBuffer sb = new StringBuffer(string);
int actualLength = length - 3;
if(sb.length() > actualLength){
if(!soft)
return sb.insert(actualLength, "...").substring(0, actualLength+3);
else {
int endIndex = sb.indexOf(" ",actualLength);
return sb.insert(endIndex,"...").substring(0, endIndex+3);
}
}
return string;
}
}
Can someone tell me where i was wrong?,
tell me if you need more explanation / code resource
Finding an inner view inside an inflated layout is among the most common operations in Android. This is usually done through a View method called findViewById(). This method will recursively go through the view tree looking for a child with a given IDcode. Using findViewById() on static UI layouts is totally fine but, as you’ve seen, ListView calls the adapter’s getView() very frequently when scrolling. findViewById() might perceivably hit scrolling performance in ListViews—especially if your row layout is non-trivial.
The View Holder pattern is about reducing the number of findViewById() calls in the adapter’s getView(). In practice, the View Holder is a lightweight inner class that holds direct references to all inner views from a row. You store it as a tag in the row’s view after inflating it. This way you’ll only have to use findViewById() when you first create the layout. Here’s the code sample with View Holder pattern applied:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.your_layout, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = convertView.getTag();
}
holder.text.setText("Position " + position);
return convertView;
}
private static class ViewHolder {
public TextView text;
}
There is one known issue with android:singleLine where it kills performance on ListViews drastically. Please see this question Why does android:singleLine="true" make ListView scrolling very laggy?
Remove it from your layout and use maxLines instead.
I have used grid view in my project
My activity layout file
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:numColumns="1"
android:stretchMode="spacingWidth"
android:layout_below="#+id/spinner1"
android:layout_marginLeft="20dip"
android:layout_marginRight="10dip"
android:layout_marginTop="20dip"
android:visibility="visible" >
</GridView>
My grid layout(grid_change_prices.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="match_parent"
android:clickable="false"
>
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#ff0000" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:background="#ffffff"
android:paddingTop="0dip" >
<TextView
android:id="#+id/cplabel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/cpprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/add_price"
android:layout_marginTop="0dp"
android:textSize="12dp"
android:inputType="number" />
</TableRow>
</TableLayout>
</RelativeLayout>
My Output from emulator
check this URL
http://i.stack.imgur.com/Uc6cs.png
My code file
public void fillgrid(int categoryid,String car){
gridView = (GridView) findViewById(R.id.gridView);
final DatabaseClass2 db = new DatabaseClass2(this);
db.openDataBase();
Cursor c = db.getprice(categoryid);
startManagingCursor(c);
String[] fromColumns = {"_id",DatabaseClass2.colPrice};
int[] toViews = {R.id.cplabel, R.id.cpprice};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.grid_change_prices, c, fromColumns, toViews);
//c.moveToNext();
gridView.setAdapter(adapter); }
Now my query is
When i click modify button, I want to update my database with the new values
i tried to get values from edit text by using .gettext() .
but it only returns value from first edittext
How i got the values from other edittexts, with the row id as well
so that i update database with new price of same design..?
Why are you using a GridView for such simple work?
better if you use ListView and it would be easier to populate the list and getting values from other EditText's.
I can give you an idea... just see this code what I'm doing in this :
public class RecordExpenseAdapter extends ArrayAdapter<HashMap<String, String>>
{
Intent intent;
Context context;
int layoutResourceId;
ArrayList<HashMap<String, String>> RecordList = new ArrayList<HashMap<String, String>>();
public RecordExpenseAdapter(Context context, int layoutResourceId,ArrayList<HashMap<String, String>> RecordList)
{
super(context, layoutResourceId, RecordList);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.RecordList = RecordList;
this.intent = new Intent(context, ScanReceipt.class);
//Log.d("ASD:","comp intialize");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
try{
if(row == null)
{
Log.d("ASD:","insdie getview");
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.categoryName = (TextView)row.findViewById(R.id.textView1);
holder.date = (TextView)row.findViewById(R.id.textView2);
holder.amount = (TextView)row.findViewById(R.id.textView3);
holder.record_rid = (TextView)row.findViewById(R.id.ridRecord);
holder.cid=(TextView)row.findViewById(R.id.cid);
holder.vendor=(TextView)row.findViewById(R.id.vendor);
holder.description=(TextView)row.findViewById(R.id.desc);
holder.attachURL=(TextView)row.findViewById(R.id.userFile);
holder.attachImage=(ImageView)row.findViewById(R.id.attachment);
holder.scanReceipt = (ImageView)row.findViewById(R.id.imageView2);
row.setTag(holder);
holder.scanReceipt.setTag(position);
holder.scanReceipt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Log.e("Scan Receipt Button List", "1");
ImageView iv = (ImageView) v;
Log.e("Scan Receipt Button List", "2");
int pos = (Integer) iv.getTag();
Log.e("Scan Receipt Button List", "3");
ScanReceipt.rid = RecordList.get(pos).get("rid").toString();
Log.e("Scan Receipt Button List", "4");
//Toast.makeText(context, "Button Clicked", Toast.LENGTH_LONG).show();
RecordExpenseAdapter.this.doIt();
}catch(Exception e){
Log.e("Error on click on Scan Receipt Button List", e.getMessage().toString());
}
}
});
// if(picture.get("userfile")=="no")
// {
// holder.attachImage.setVisibility(View.INVISIBLE);
// }
// else
// {
// holder.attachImage.setVisibility(View.VISIBLE);
// }
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
HashMap<String, String> picture = RecordList.get(position);
holder.categoryName.setText(picture.get("category_name"));
holder.date.setText(picture.get("date"));
holder.amount.setText(picture.get("amount"));
holder.record_rid.setText(picture.get("rid"));
holder.cid.setText(picture.get("cid"));
holder.vendor.setText(picture.get("vendor"));
holder.description.setText(picture.get("description"));
holder.attachURL.setText(picture.get("image"));
Log.e("Record Expense Adapter 0 : IMAGE", picture.get("image").toString());
Log.e("RID : " + position, picture.get("rid").toString());
// Log.e("error value", picture.get("userfile"));
//Log.e("error value", "value is error");
// if(picture.get("userfile")=="no")
// {
// holder.attachImage.setVisibility(View.INVISIBLE);
// }
//
// else
// {
// holder.attachImage.setVisibility(View.VISIBLE);
// }
if(picture.get("image").toString().trim().equalsIgnoreCase("no")){
holder.scanReceipt.setVisibility(View.VISIBLE);
holder.attachImage.setVisibility(View.INVISIBLE);
}else{
holder.scanReceipt.setVisibility(View.GONE);
holder.attachImage.setVisibility(View.VISIBLE);
}
}
catch(Exception e){
}
return row;
}
public void doIt(){
Log.e("Adapter 0", "In do It");
context.startActivity(intent);
}
static class ImageHolder
{
TextView categoryName;
TextView date;
TextView amount;
TextView record_rid;
TextView vendor;
TextView cid;
TextView description;
TextView attachURL;
ImageView attachImage,scanReceipt;
}
}
in above code, I have implemented the click listener on ImageView scanReceipt. It's the same imageview each time i.e. the same layout is inflated for each row, which means the same id is inflated for all the rows but still this click event will perform a different task for different row.
what you need to do is : simply use the EditText as I'm using the ImageView. Implement the TextWatcher as I'm implementing the click listener.
you have to use custom list for that say this-
List<CustomClass> list = new ArrayList<CustomClass>();
in this custom class, put the public variables so that you can access those by object of CustomClass and add that object to this above created list.
Once you get the values in this list, you can use it any way you want.
I hope it would help... :)
#Prakash
thanxx for idea dude
Now, I attached textwatcher to edittext field and managed to get rowid
with the help of this
price_text.setTag(c.getPosition());
And retreive that position in afterTextChanged
price_text.getTag()
But now how could i save that values Like you said in above post....
I extended an ArrayAdapter with an overridden getView() method to fill my ListView object, but the adapter will grab the first two or three objects from the ArrayList I pass to it, then just repeat those in seemingly random order for the entire length of the list. Furthermore, when I scroll up and down in the ListView, some rows change from one list item to another. The ArrayList<Credit> object gets populated from a JSONObject, and to the best of my knowledge is correct prior to being passed into the ArrayAdapter.
My custom ArrayAdapter
private class CreditArrayAdapter extends ArrayAdapter<Credit> {
private ArrayList<Credit> credits;
private int creditCount;
public CreditArrayAdapter(Context ctx, int textViewResourceId, List<Credit> items) {
super(ctx, textViewResourceId, items);
credits = (ArrayList<Credit>) items;
creditCount = credits.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_movie_medium, null);
Credit current = credits.get(position);
Log.d(tag, "current credit: " + current.toString());
ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
try {
creditPicture.setImageBitmap(current.getPosterSmall());
} catch(Exception e) {
Log.d(tag, "failed to set cast member picture");
e.printStackTrace();
}
TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
castMemberName.setText(current.toString());
}
return v;
}
#Override
public int getCount() {
return creditCount;
}
}
being called by:
appearsIn = (ListView) findViewById(R.id.listview_appears_in);
List<Credit> credits = searcher.getCreditsByPersonId(personId);
CreditArrayAdapter creditAdapter = new CreditArrayAdapter(getBaseContext(), R.layout.list_item_movie_medium, credits);
creditAdapter.notifyDataSetChanged();
appearsIn.setAdapter(creditAdapter);
The layout containing the ListView:
<?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="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:weightSum="2">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView android:id="#+id/image_person_info"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="person image"/>
<TextView android:id="#+id/text_person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"/>
<TextView android:id="#+id/text_person_birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView android:id="#+id/text_person_death"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView android:id="#+id/text_person_bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"/>
</LinearLayout>
</ScrollView>
<ListView android:id="#+id/listview_appears_in"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
My list item layout:
<?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">
<ImageView android:id="#+id/image_poster_thumbnail"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="#+id/text_credit_info"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
change the getView like this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_movie_medium, null);
}
Credit current = credits.get(position);
Log.d(tag, "current credit: " + current.toString());
ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
try {
creditPicture.setImageBitmap(current.getPosterSmall());
} catch(Exception e) {
Log.d(tag, "failed to set cast member picture");
e.printStackTrace();
}
TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
castMemberName.setText(current.toString());
return v;
}
Your problem was you were not doing all the code you needed to set the View object correctly when convertView was != null.
Use this snippet:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_movie_medium, null);
}
Credit current = credits.get(position);
Log.d(tag, "current credit: " + current.toString());
ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
try {
creditPicture.setImageBitmap(current.getPosterSmall());
} catch(Exception e) {
Log.d(tag, "failed to set cast member picture");
e.printStackTrace();
}
TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
castMemberName.setText(current.toString());
return v;
}
try adjust your xml layout on anyone place that references the height, because the android will do a measure to draw eachtime and will redraw the cell up again.
Try use on listview match_parent and on cell at row an exactly height.
sorry my bad english.
My problem is that my listView repeat all items of my layout.
I need to have the edit text and the button at the bottom of this listView.
But here the edit text and button is repeated for each row of listView and i don't know why.
If someone could help me
The activity :
public class MessActivity extends ListActivity
{
public Channel chan;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(Network.getInstance().Messages);
Bundle b = getIntent().getExtras();
this.chan = (Channel) b.get("chanSelected");
//add all message of selected chan to the messAdapter
for (int i = 0 ; i < this.chan.getListMessage().size() ; i++) {
Message mess = this.chan.getListMessage().get(i);
Network.getInstance().addMessage(mess);
}
setContentView(R.layout.mess_list);
}
}
The adapter :
public class MessageAdapter extends ArrayAdapter<Message>
{
private Context context;
private int textViewResourceId;
public MessageAdapter(Context context, int textViewResourceId)
{
super(context, textViewResourceId);
this.context = context;
this.textViewResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
MessHolder holder = null;
//row null
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder = new MessHolder();
holder.texte = (TextView)row.findViewById(R.id.listMess);
row.setTag(holder);
}
else
{
holder = (MessHolder)row.getTag();
}
Message mess = getItem(position);
holder.texte.setText(mess.getText());
return row;
}
static class MessHolder
{
TextView texte;
}
}
The layout :
<?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="wrap_content"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list">
</ListView>
<TextView
android:id="#+id/listMess"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
//edittext
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Envoyer" />
</LinearLayout>
</LinearLayout>
Thank you for your help.
Use ListView method addFooter(View v).
You can define xml layout for this view and inflate it:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
Firstly Reason of this issue is Listview Reuses its view when scrolls , So we should have a logic in our get view to manage that
if(convertview == null)
{
//code here
}else {
//code here
}
, but if you dont want to reuse then you can use following :
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}