Will I don\'t what is the problem. There are no errors nothing, and the program working perfectly. However, when I click on an item on list View it does not do anything. In
At first use ViewHolder
pattern. At second extend BaseAdapter
not AdapterView
. At third delete android:clickable="true"
from row.xml
. Clickable state must have ListView
in method onItemClickListener
, not own row.
Updated
For the Customized listview you can use BaseAdapter.
And With Adapter Class you it have some tips to make it smooth , for that you can refer this
Its also about resusability already created row's control instances.For that ViewHolder pattern i prefer to use to hold the objects. Please check this for more detail
Which is missing in your code of the adapter's getView Method.
For more explanation i leave comment in Adapter's getView Method.
Class to hold the details if you are gng to use thn naming convention should be proper here first the name is "Object" which is wrong.
Activity
private ListView list;
private final ArrayList<RestaurantsDetails> pairs = new ArrayList<RestaurantsDetails>();
private Activity context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pairs.add(new RestaurantsDetails("McDonald's", "mcd"));
pairs.add(new RestaurantsDetails("Subway", "mcd"));
pairs.add(new RestaurantsDetails("Pizza Hut", "mcd"));
pairs.add(new RestaurantsDetails("Burger King", "mcd"));
list = (ListView) findViewById(R.id.listview);
AdapterViewCustom adapter = new AdapterViewCustom(this, pairs);
list.setAdapter(adapter);
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(android.widget.AdapterView<?> parent,
View view, int position, long id) {
Toast.makeText(getApplicationContext(),
pairs.get(position).name, Toast.LENGTH_LONG).show();
}
});
}
DataLayer Class
/**
* Use proper name For the class. Should not use the name like "Object" or
* the class which is already used by framework
*
*/
public class RestaurantsDetails {
public String name;
public String img;
public RestaurantsDetails(String name, String img) {
this.name = name;
this.img = img;
}
}
Adapter
public class AdapterViewCustom extends BaseAdapter {
private Activity context_1;
private ArrayList<RestaurantsDetails> pairs;
public AdapterViewCustom(Activity context,
ArrayList<RestaurantsDetails> pairs) {
context_1 = context;
this.pairs = pairs;
}
@Override
public int getCount() {
return pairs.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context_1).inflate(
R.layout.custom_row, null);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView
.findViewById(R.id.log_img);
viewHolder.txt = (TextView) convertView
.findViewById(R.id.tv_view);
/**
* At very first time when the List View row Item control's
* instance is created it will be store in the convertView as a
* ViewHolder Class object for the reusability purpose
**/
convertView.setTag(viewHolder);
} else {
/**
* Once the instance of the row item's control it will use from
* already created controls which are stored in convertView as a
* ViewHolder Instance
* */
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt.setText(pairs.get(position).name);
int id = context_1.getResources().getIdentifier(
pairs.get(position).img, "drawable",
context_1.getPackageName());
viewHolder.img.setImageResource(id);
return convertView;
}
public class ViewHolder {
public final ImageView img;
public final TextView txt;
}
}