Custom Adapter ViewHolder issue - java

I'm trying to use a custom adapter for a list. This list has 2 types of rows but I only use one layout (keeping the items that I don't need with visibility set to View.GONE). However I keep getting a Class cast exception when trying to access the editbox-style row. I have very little experience with custom adapters. Your help is really appreciated :D
Here's the code(I removed the setonclicklisteners to keep it short):
public class SubEventListAdapter extends ArrayAdapter<MobileSubEvent>
{
private ArrayList<MobileSubEvent> _items;
private Context _context;
public SubEventListAdapter(Context context, ArrayList<MobileSubEvent> items)
{
super(context, R.layout.view_select_event_item3, items);
this._items = items;
this._context = context;
}
static class ViewHolder
{
TextView text;
ImageButton imagebutton;
ImageView check;
EditText editText;
Button button;
}
#Override
public int getCount()
{
return this._items.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
final ViewHolder viewHolder;
final MobileSubEvent event = _items.get(position);
if (v == null)
{
LayoutInflater _inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = _inflater.inflate(R.layout.view_select_event_item3, null);
viewHolder = new ViewHolder();
viewHolder.imagebutton = (ImageButton) v.findViewById(R.id.ibNext);
viewHolder.text = (TextView) v.findViewById(R.id.EVENT);
viewHolder.check = (ImageView) v.findViewById(R.id.ivCheck);
viewHolder.button = (Button) v.findViewById(R.id.bScanOrSign);
viewHolder.editText = (EditText) v.findViewById(R.id.etInput);
v.setTag(viewHolder);
} else
{
v = convertView;
viewHolder = (ViewHolder) v.getTag(); //here is where the class cast exception occurs
}
if (viewHolder.text != null)
viewHolder.text.setText(this._items.get(position).get_description());
v.setTag(this._items.get(position));
...
return v;
}
Logcat:
view_select_event_item3:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dip" >
<ImageView
android:id="#+id/ivCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:background="#drawable/check"
android:focusable="false"
android:visibility="invisible" />
<TextView
android:id="#+id/EVENT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/ivCheck"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="Sample text"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
/>
<EditText
android:id="#+id/etInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="130dp"
android:maxWidth="165dp"
android:layout_toRightOf="#id/ivCheck"
android:background="#android:drawable/editbox_background"
android:padding="5dp"
android:hint="Hint text"
android:singleLine="true"
android:text=""
android:textSize="20sp"
android:visibility="invisible"
/>
<Button
android:id="#+id/bScanOrSign"
android:layout_width="62dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#id/ibNext"
android:text="Scan"
android:visibility="invisible"
/>
<ImageButton
android:id="#+id/ibNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:background="#drawable/right"
android:visibility="invisible" />
</RelativeLayout>

Try this in the getView, check for class name of the convertView object (The View Holder object) if it does not matches class name of your ViewHolder (in my case it is VisitsListViewHolder) - set it as null so new instance is created for the view holder
if(convertView != null)
{
String className = convertView.getTag().getClass().getName();
if(!className.equalsIgnoreCase("VisitsListViewHolder"))
{
convertView = null;
}
}

Related

Android Custom Adapter - Setting an Image relative to information in Listview

I'm currently struggling with a function I want to have in my application. The users Incomes are shown in a ListView. There are two types of incomes, normal "income" and "other income", this is selected in the application when the user submits the income to a SQLite Database that he or she receives. Now - I want to present the different incomes in a ListView with an image in the ListView relative to the type of category (two types of income category - "income" and "other income".
I cant figure out how I should do this ... can you do a switch-statement of some sort!?
My XML for the Custom ListView looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<ImageView
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_vertical"
android:id="#+id/icon1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="48dp"
android:layout_weight="33.3">
<TextView
android:text="TextView1"
android:textSize="18dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="#+id/text1"/>
<TextView
android:text="Textview2"
android:textSize="14dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:="#id/text2"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="vertical">
<TextView
android:text="Textview3"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity=""
android:gravity="center_vertical"
android:id="#+id/text3"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp">
<ImageView
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_vertical"
android:id="#+id/icon2"/>
</LinearLayout>
</LinearLayout>
And my AdapterClass looks like this:
public class CustomIncomeListAdapter extends ArrayAdapter<IncomeModel> {
private ArrayList<IncomeModel> incomeList;
private Activity context;
private ViewHolder viewHolder;
int[] incomeIcon = {R.drawable.income, R.drawable.otherincome};
int[] forwardIcon = {R.drawable.forward};
public CustomIncomeListAdapter(Activity context, int[] incomeIcon, ArrayList<IncomeModel> incomeList) {
super(context, R.layout.adapter_view_layout, incomeList);
this.incomeList = incomeList;
this.context = context;
this.incomeIcon = incomeIcon;
}
private static class ViewHolder {
TextView incomeReference;
TextView incomeDate;
TextView incomeAmount;
ImageView incomeIcon;
ImageView forwardIcon;
}
public View getView(int position, View convertView, ViewGroup parent) {
IncomeModel incomeModel = getItem(position);
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.adapter_view_layout, null, true);
viewHolder.incomeReference = (TextView) convertView.findViewById(R.id.text1);
viewHolder.incomeDate = (TextView) convertView.findViewById(R.id.text2);
viewHolder.incomeAmount = (TextView) convertView.findViewById(R.id.text3);
viewHolder.incomeIcon = (ImageView) convertView.findViewById(R.id.icon1);
viewHolder.forwardIcon = (ImageView) convertView.findViewById(R.id.icon2);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.incomeReference.setText(incomeModel.getIncomeCategory());
viewHolder.incomeDate.setText(incomeModel.getIncomeDate());
viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (...?) {
}
}
viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (income) {
//define your income to 1 for other income
case 1:
incomeIcon.setBackgroundResource(R.drawable.otherincome);
break;
default:
incomeIcon.setBackgroundResource(R.drawable.income);
break;
}
you can check type , if type is income means set corresponding image else set other image

how to hide and show image in listview in android?

I have one list in which I have one image view .I need to show that image only when my one variable is true .Actually I make a custom adapter in which I have one variable "serviceAlertPresent" .If it is true I need to show image elese I don't want to show image on row.How I Will acheive this
Here is my code of each row .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/transparent"
android:padding="5dp" >
<TextView
android:id="#+id/item_platform"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="Ptf."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/item_schDepart"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/item_platform"
android:layout_alignBottom="#+id/item_platform"
android:layout_toRightOf="#+id/item_platform"
android:text="dpt"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/item_arrival"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/item_schDepart"
android:layout_alignBottom="#+id/item_schDepart"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/item_expectdepart"
android:text="Arr."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/item_expectdepart"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_below="#+id/item_schDepart"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/item_platform"
android:text="Expert"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/item_expertarrival"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/item_expectdepart"
android:layout_alignBottom="#+id/item_expectdepart"
android:layout_alignLeft="#+id/item_arrival"
android:text="exprt Arrival"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/item_stationName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/item_arrival"
android:layout_alignBottom="#+id/item_arrival"
android:layout_marginLeft="17dp"
android:layout_toRightOf="#+id/item_expertarrival"
android:text="Des."
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/alertStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:layout_toRightOf="#+id/item_stationName"
android:src="#drawable/alert_icon" />
</RelativeLayout>
here is custom adapter
public class CustomListAdapter extends BaseAdapter{
ArrayList<deparaturedaseboarddto> deparaturedaseboarddto;
private Context context;
public CustomListAdapter( Context context, ArrayList<deparaturedaseboarddto> deparaturedaseboarddto){
this.deparaturedaseboarddto=deparaturedaseboarddto;
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return deparaturedaseboarddto.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return deparaturedaseboarddto.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.row_listitem, null);
}
final TextView platFormName = (TextView) v
.findViewById(R.id.item_platform);
final TextView schDepart = (TextView) v
.findViewById(R.id.item_schDepart);
final TextView expDepart = (TextView) v
.findViewById(R.id.item_expectdepart);
final TextView arrival = (TextView) v
.findViewById(R.id.item_arrival);
final TextView exparrival = (TextView) v
.findViewById(R.id.item_expertarrival);
final TextView stationName = (TextView) v
.findViewById(R.id.item_stationName);
final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
final String schExpectValue= deparaturedaseboarddto.get(position).getExpDepart();
final String arrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
final String exparrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
String stationNameValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();
platFormName.setText(platformValue);
schDepart.setText(schDepartValue);
expDepart.setText(schExpectValue);
arrival.setText(arrivalValue);
exparrival.setText(exparrivalValue);
stationName.setText(stationNameValue);
return v;
}
public void referhList( ArrayList<deparaturedaseboarddto> deparaturedaseboarddto){
this.deparaturedaseboarddto=deparaturedaseboarddto;
notifyDataSetChanged();
}
}
I am asking this image view
<ImageView
android:id="#+id/alertStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:layout_toRightOf="#+id/item_stationName"
android:src="#drawable/alert_icon" />
I need to show this when "serviceAlertPresent" .I will get value like that deparaturedaseboarddto.get(position).isServiceAlertPresent()
In the adapter get the imageView and set the visibilite to INVISIBLE or VISIBLE.
final ImageView alertStatus = (ImageView) v
.findViewById(R.id.alertStatus);
if (condition){
alertStatus.setVisibility(View.VISIBLE);
}
else{
alertStatus.setVisibility(View.INVISIBLE);
}
And when the condition change, you should call notifyDataSetChanged() of your adapter.
Hope it helps you!
You can check boolean values of serviceAlertPresent inside getView() method itself.
If it's true then use mImageView.setVisibility(View.VISIBLE) else remove the presence of it by using mImageView.setVisibility(View.GONE).
Note: When you use GONE, it will be invisible and at the same time it will not take any space, where as when you use INVISIBLE it will be invisible and will take space in layout.
In ImageView, set android:setVisiblity="gone"; by default.
show that image only when your variable is true.
like,
imageView.setVisibility(View.GONE);
final ImageView alertStatus= (ImageView) v.findViewById(R.id.alertStatus);
v.alertStatus.setVisibility(View.INVISIBLE);

how can i change fontcolor and font type of my listview

I have one activity that contains text view, buttons, spinner and list view
my main XML file contains :
<RelativeLayout 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:background="#ffff00"
tools:context="com.example.taxitabriz.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="#string/app_name"
android:textSize="35dp"
android:textStyle="bold"
android:textColor="#996600"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/linearlayout1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#996600"
android:text="#string/exit"
android:background="#drawable/backbutton" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#996600"
android:text="#string/about"
android:background="#drawable/backbutton" />
</LinearLayout>
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="19dp" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_above="#+id/linearlayout1"
android:layout_alignParentRight="true"
android:layout_weight="49.94" >
</ListView>
and part of my java code is here:
ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
lv1.setAdapter(ard1);
but i can't change font color or font type in list view or spinner. how can i do it?
You need to create a CustomListAdapter.
private class CustomListAdapter extends ArrayAdapter {
private Context mContext;
private int id;
private List <String>items ;
public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}
#Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
TextView text = (TextView) mView.findViewById(R.id.textView);
if(items.get(position) != null )
{
text.setTextColor(Color.WHITE);
text.setText(items.get(position));
text.setBackgroundColor(Color.RED);
int color = Color.argb( 200, 255, 64, 64 );
text.setBackgroundColor( color );
}
return mView;
}
}
The list item looks like this (custom_list.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>
Use the TextView api's to decorate your text to your liking
and you will be using it like this
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
If you really want to use androids simple list layout, we see that they declare their textview's identifier as such:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
So just make your custom adapter, inflate their layout & find that text view's id. Something like the following:
public final class CustomAdapter extends ArrayAdapter<String> {
private Context context;
ViewHolder holder;
private ArrayList<String> messages;
public CustomAdapter(Context context, ArrayList<String> data) {
this.context = context;
this.messages = data;
Log.d("ChatAdapter", "called constructor");
}
public int getCount() {
return messages.size();
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simple_dropdown_item_1line, null);
holder = new ViewHolder();
holder.message = (TextView) convertView
.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.message.setText(data.get(position));
holder.message.setTextColor(Color.BLUE);
// don't do this, remember the face variable once just showing explicitly for u
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
holder.message.setTypeface(face);
return convertView;
}
public static class ViewHolder {
public TextView message;
}
}
Edit: The following is how you would use the custom array adapter in your activity.
// I'm not sure if your sp or lv1 wants the adapter, but
// whatever you require your list view's data to be just
// set the custom adapter to it
CustomAdapter<String> myAdapter = new ArrayAdapter<String>(this, s1);
lv1=(ListView) findViewById(R.id.listView1)
lv1.setAdapter(myAdapter);
You can do like this, add a ListView item in xml,at res/layout/list_item1.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff"
android:testStyle="italic"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
then,
ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);// **change R.layout.list_item1**
lv1.setAdapter(ard1);
You have to put the .ttf file of font(which you want to add ) in asstes/fonts/ folder and write code in onCreate method like below. i have puted Chalkduster.ttf in my asstes/fonts/ folder
TextView txttest = (TextView) findViewById(R.id.txttest);
Typeface custom_font = Typeface.createFromAsset(getAssets(),
"fonts/Chalkduster.ttf");
txttest.setTypeface(custom_font);
You can handle clicks of selected item by write code just like below
ListView lvNews = (ListView) findViewById(R.id.lvNews);
lvNews.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// Write your code here
//int newsId = newsArray.get(position).getId();
//Intent i = new Intent(NewsListActivity.this,
// NewsDetailActivity.class);
//i.putExtra(Constants.NEWS_ID, newsId);
//i.putExtra("isFromNotification", false);
//startActivity(i);
}
});

First progressBar in listview doesn't progress

I have a very bad issue.
I have a listview with an adapter.
public class MediaAdapter extends ArrayAdapter<Media> {
public MediaAdapter(Context context, Media[] items) {
super(context, R.layout.mediaitemlist, items);
}
/* private view holder class */
private class ViewHolder {
TextView media_name;
TextView media_path;
TextView media_version;
TextView media_type;
ProgressBar pb;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View view = convertView;
if (view == null) {
view = (View)LayoutInflater.from(getContext()).inflate(R.layout.mediaitemlist, null);
holder = new ViewHolder();
holder.media_name = (TextView) view
.findViewById(R.id.media_name);
holder.media_path = (TextView) view
.findViewById(R.id.media_path);
holder.media_type = (TextView) view
.findViewById(R.id.media_type);
holder.media_version = (TextView) view
.findViewById(R.id.media_version);
holder.pb = (ProgressBar)view.findViewById(R.id.itemprogressdl);
view.setTag(holder);
//Maybe the problem is here ???????????
getItem(position).setmProgressBar((ProgressBar)view.findViewById(R.id.itemprogressdl));
} else {
holder = (ViewHolder) view.getTag();
}
Media rowItem = (Media) getItem(position);
holder.media_name.setText(rowItem.get_Name());
holder.media_path.setText(rowItem.get_Path());
holder.media_type.setText(rowItem.get_TypeString());
holder.media_version.setText("" + rowItem.get_Version());
rowItem.setmProgressBar(holder.pb);
return view;
}
}
Here, the mediaitemlist Layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#drawable/listitembackground" >
<TextView
android:id="#+id/media_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black"
android:textSize="25sp" />
<TextView
android:id="#+id/media_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/media_name"
android:textColor="#android:color/black"
android:visibility="invisible" />
<TextView
android:id="#+id/media_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/media_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/media_version"
android:textColor="#android:color/black" />
<ProgressBar
android:id="#+id/itemprogressdl"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/media_name"
android:layout_below="#+id/media_name"
android:layout_toLeftOf="#+id/media_version"
android:max="100"
android:visibility="invisible"
android:indeterminate="true"/>
</RelativeLayout>
The progressBar is an attribute of a media class. One Media class have is own progress according to his place in the listView.
But, all the progressBar attributes are good, except the first of the listView. If I click on the second or over, I can see the progressBar progresses, but not with the first.
So, why the first progressBar doesn't work and the next work?
If you need more code,comments or other, ask me.
try moving this line
getItem(position).setmProgressBar((ProgressBar)view.findViewById(R.id.itemprogressdl));
out of the if..else condition.
Try this answer:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View view = convertView;
Media rowItem = (Media) getItem(position);
if (view == null) {
view = (View)LayoutInflater.from(getContext()).inflate(R.layout.mediaitemlist, null);
holder = new ViewHolder();
holder.media_name = (TextView) view
.findViewById(R.id.media_name);
holder.media_path = (TextView) view
.findViewById(R.id.media_path);
holder.media_type = (TextView) view
.findViewById(R.id.media_type);
holder.media_version = (TextView) view
.findViewById(R.id.media_version);
holder.pb = (ProgressBar)view.findViewById(R.id.itemprogressdl);
view.setTag(holder);
//Maybe the problem is here ???????????
getItem(position).setmProgressBar((ProgressBar)view.findViewById(R.id.itemprogressdl));
} else {
holder = (ViewHolder) view.getTag();
rowItem.setmProgressBar(holder.pb);
}
holder.media_name.setText(rowItem.get_Name());
holder.media_path.setText(rowItem.get_Path());
holder.media_type.setText(rowItem.get_TypeString());
holder.media_version.setText("" + rowItem.get_Version());
return view;
}

make links clickable in custom listview android

I created a listview wit custom addapter to display tweets from a user. Now sometimes in the tweet there is a link provided. I want them to be clickable, and the item itself from the listview doesn't have to be clickable. this is the layout of my listview with 2 rows to show an example
I have tried to make the links in the textview(text with sometimes links) clickable and also autolink: WEB in XML and programatically. But every time i click on the link it selects the listitem en not the link in the textview.
In java code it was like this :
message.setAutoLinkMask(1);
message.setLinksClickable(true);
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int textViewResourceId,
ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
#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.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView message = (TextView) v.findViewById(R.id.message);
**message.setAutoLinkMask(1);
message.setLinksClickable(true);**
ImageView image = (ImageView) v.findViewById(R.id.avatar);
TextView date = (TextView) v.findViewById(R.id.date);
if (username != null) {
username.setText(tweet.username);
}
if (message != null) {
message.setText(tweet.message);
}
if (date != null) {
date.setText(tweet.hfdate);
}
}
return v;
}
}
and the xml for the listitem is like this in the textview message like this
android:autoLink="web" android:linksClickable="true"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px"
tools:context=".TwitterActivity$AsyncOp" >
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/twitterimg" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/Orange" />
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:autoLink="web"
android:linksClickable="true"
android:textColor="#0099CC" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#color/Orange" />
</LinearLayout>
</LinearLayout>
Anybody an idea wath I am doing wrong?

Categories