经历了许多演示,并且无法弄清楚如何在列表视图中使用imageview。 尝试了代码但是在listview中看不到图像。其他详细信息在列表视图中正常工作 请帮忙。
以下是代码 主要活动有tabhost。在第二个选项卡下,列表视图存在。 listdata是我在列表中需要的数据。 MyBaseAdapter用于在listview中加载数据所需的适配器。
MainActivity.java
package com.example.trial;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView list;
String[] dish={"Baby Corn Satay","Dum Aloo","Kadai Paneer","Methi Mutter Malai","Paneer Butter Masala","Saag Panner","Veg Kolhapuri"};
String[] about={"Baby Corn with marinade in a skewer","Baby Poatatoes in a spicy gravy","Kadhai paneer","Methi and peas in malai","Cottage cheese in butter masala","Cottage cheese in gravy","Spicy veg mix"};
String[] dprice={"250","300","350","250","350","250","300"};
Integer[] icon={R.drawable.baby_corn_satay,R.drawable.dum_aloo,R.drawable.kadai_paneer,R.drawable.methi_mutter_malai,R.drawable.paneer_butter_masaala,R.drawable.saag_paneer,R.drawable.veg_kolhapuri};
ArrayList<ListData> myList=new ArrayList<ListData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th=(TabHost) findViewById (R.id.tabhost);
th.setup();
TabSpec specs=th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Beverages");
th.addTab(specs);
list=(ListView)findViewById(R.id.list);
getInList();
list.setAdapter(new MyBaseAdapter(MainActivity.this,myList));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +dish[+ position], Toast.LENGTH_SHORT).show();
}
});
specs=th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Cuisines");
th.addTab(specs);
specs=th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Desserts");
th.addTab(specs);
specs=th.newTabSpec("tag4");
specs.setContent(R.id.tab4);
specs.setIndicator("Tri");
th.addTab(specs);
}
private void getInList(){
for(int i=0;i<7;i++)
{
ListData ld=new ListData();
ld.setAbout(about[i]);
ld.setDish(dish[i]);
ld.setDprice(dprice[i]);
ld.seticon(icon[i]);
myList.add(ld);
}
}
}
MainActivity.xml
<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"
tools:context=".MainActivity" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="529dp"
android:layout_marginTop="221dp"
android:text="Tab1"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="TooDeepLayout" >
</ListView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="529dp"
android:layout_marginTop="221dp"
android:text="Tab3"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<Button
android:id="@+id/button44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="529dp"
android:layout_marginTop="221dp"
android:text="tab4"
tools:ignore="HardcodedText" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
MyBaseAdapter.java
package com.example.trial;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList=new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context,ArrayList<ListData> myList){
this.myList=myList;
this.context=context;
inflater=LayoutInflater.from(this.context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
@Override
public ListData getItem(int position) {
// TODO Auto-generated method stub
return myList.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
MyViewHolder mvh;
if(convertView==null)
{
convertView=inflater.inflate(R.layout.dishview,null);
mvh=new MyViewHolder();
convertView.setTag(mvh);
}
else
{
mvh=(MyViewHolder) convertView.getTag();
}
mvh.dish = detail (convertView,R.id.dish,myList.get(position).getDish());
mvh.dprice= detail (convertView,R.id.price,myList.get(position).getDprice());
mvh.about = detail (convertView,R.id.about,myList.get(position).getAbout());
mvh.icon= idetail(convertView,R.id.icon,myList.get(position).geticon());
NumberPicker np = (NumberPicker) convertView.findViewById(R.id.qty);
np.setMaxValue(9);
np.setMinValue(1);
np.setValue(1);
Button b = (Button) convertView.findViewById(R.id.b1);
b.setTag(convertView);
return convertView;
}
private ImageView idetail(View v,int resId,int icon)
{
ImageView iv=(ImageView) v.findViewById(resId);
iv.setImageResource(resId);
return iv;
}
private TextView detail(View v,int resId,String text)
{
TextView tv=(TextView)v.findViewById(resId);
tv.setText(text);
return tv;
}
private static class MyViewHolder{
TextView about,dish,dprice;
ImageView icon;
}
}
ListData.java
package com.example.trial;
import android.widget.NumberPicker;
public class ListData {
String dish;
String about;
String dprice;
Integer icon;
public String getDish() {
return dish;
}
public void setDish(String dish) {
this.dish = dish;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String getDprice() {
return dprice;
}
public void setDprice(String dprice) {
this.dprice = dprice;
}
public Integer geticon() {
return icon;
}
public void seticon(Integer imageId) {
this.icon = imageId;
}
}
dishview.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<TextView
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/icon"
android:layout_alignLeft="@+id/dish"
android:text="Discription"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/dish"
android:layout_marginLeft="49dp"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:src="@drawable/ic_launcher"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/dish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:layout_marginTop="27dp"
android:layout_toRightOf="@+id/icon"
android:text="Dish name"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/about"
android:layout_marginRight="146dp"
android:layout_toLeftOf="@+id/qty"
android:text="Price"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<NumberPicker
android:id="@+id/qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:ignore="NewApi" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/price"
android:layout_marginLeft="41dp"
android:layout_toRightOf="@+id/qty"
android:text="Add"
tools:ignore="HardcodedText" />
</RelativeLayout>
请帮助我。 感谢您的期待。
答案 0 :(得分:0)
刚改变布局位置并停止工作
清洁并重建。可能您的二进制XML文件中的资源ID和R.java
不同步。
现在您已经包含了logcat,java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
中的detail()
也表明了这一点 - 您传递给该方法的资源ID有效TextView
但至少在一种情况是指二进制XML中的ImageView
。
答案 1 :(得分:0)
你能粘贴你的activity_main.xml吗?此外,您最好使用操作栏标签。
答案 2 :(得分:0)
错误在MyBaseAdapter.java中 简单的错误,参数不匹配。
private ImageView idetail(View v,int resId,int icon)
{
ImageView iv=(ImageView) v.findViewById(resId);
iv.setImageResource(resId);
return iv;
}
我们需要将其更改为
private ImageView idetail(View v,int resId,int icon)
{
ImageView iv=(ImageView) v.findViewById(R.id.icon);
iv.setImageResource(icon);
return iv;
}