Android:listview错误打印中的多个textviews

时间:2012-04-30 15:53:26

标签: android listview textview

我的问题如下:使用此example,我尝试为列表视图创建一个自定义适配器,从数据库中获取2 ArrayLists(名称和数字)并将其数据输出到列表中的textview。数据显示在列表中,但是它们会单独显示,这意味着名称会显示为数字,您可以单独单击它们,而不是将两者一起作为列表中的一个条目。附上是必要的代码。

我的适配器类:

public class ListViewCustomAdapter extends BaseAdapter {  

    public Activity context;  
    public LayoutInflater inflater; 
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> nos = new ArrayList<String>();


    public ListViewCustomAdapter(Activity context,ArrayList<String> names, ArrayList<String> nos) {  
        super();  

        this.context = context;  
        this.names = names;
        this.nos=nos;

        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }
    public int getCount() {  
        // TODO Auto-generated method stub  
        return names.size();  
    }  

    public Object getItem(int position) {  
        // TODO Auto-generated method stub  
        return null;  
    }  

    public long getItemId(int position) {  
        // TODO Auto-generated method stub  
        return 0;  
    }  

    public static class ViewHolder  
    {  
        TextView txtViewName;  
        TextView txtViewNo;  
    }  
    public View getView(int position, View convertView, ViewGroup parent) {  
        // TODO Auto-generated method stub  

        ViewHolder holder;  
        if(convertView==null)  
        {  
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.editlist, null);  

           // holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);  
            holder.txtViewName = (TextView) convertView.findViewById(R.id.textView1);  
            holder.txtViewNo = (TextView) convertView.findViewById(R.id.textView2);  

            convertView.setTag(holder);  
        }  
        else  
            holder=(ViewHolder)convertView.getTag();  

        //holder.imgViewLogo.setImageResource(R.drawable.icon);  
        holder.txtViewName.setText(names.get(position));  
        holder.txtViewNo.setText(nos.get(position));  

        return convertView;  
    }   


}

listview xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  
    android:id="@+id/relativeLayout1"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:padding="5dip">  


    <TextView  
        android:textAppearance="?android:attr/textAppearanceLarge"  
        android:layout_height="wrap_content"  
        android:text="TextView"  
        android:layout_width="wrap_content"  
        android:id="@+id/textView1"   
        android:layout_marginLeft="2dip">  
    </TextView>  

    <TextView  
        android:layout_height="wrap_content"  
        android:text="TextView"  
        android:layout_width="wrap_content"  
        android:id="@+id/textView2"  
        android:layout_toRightOf="@+id/textView1"   
        android:layout_marginLeft="2dip">  
    </TextView>    

</RelativeLayout>  

使用适配器的主要活动:

public class EditActivity extends ListActivity implements OnClickListener, OnItemClickListener{
    Button b_add;
    ListView lv;
    SQLUtils sqlUtils = new SQLUtils(this);
     ListViewCustomAdapter adapter; 
     ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> nos = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.edit_layout);

         b_add = (Button)findViewById(R.id.b_add);
         b_add.setOnClickListener(this);
         lv = (ListView)findViewById(android.R.id.list);
         names=sqlUtils.getActions(1);
         nos=sqlUtils.getActions(2);

         //lv.setOnItemClickListener(this);
         adapter = new ListViewCustomAdapter(this, names, nos);  
            lv.setAdapter(adapter); 
    }
[1]

2 个答案:

答案 0 :(得分:0)

我对您的问题感到困惑,但是看一下教程和代码中的差异,我会做出 blind 猜测。

您扩展ListActivity而不是像示例那样的常规Activity,对于ListActivity,您可以像这样定义onListItemClick侦听器:

public void onListItemClick(AdapterView<?> parent, View view, int position, long id) {
    String title = ((TextView) view.findViewById(R.id.textView1)).getText().toString();
    String description = ((TextView) view.findViewById(R.id.textView2)).getText().toString();
    Toast.makeText(this, "Title => " + title + " => n Description " + description, Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

如果您希望适配器中的每个项目都包含两个TextView,请使ViewHolder类扩展LinearLayout并将其设置为垂直。或者,您可以使用一个TextView而不是您自己的类/ linearlayout,并使其成为多行,然后将文本设置为name +“\ n”+ number