Use external XML files for custom list view adapters?

I am currently developing my first Android application. In the application, there should be a listview, which displays pictures on the left and two textviews next to it

The XML of listview is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:padding="6dip" >

<ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

<TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />

<TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

</RelativeLayout>

The XML of mainactivity looks like this:

<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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">

<ListView xmlns:android="rowlayout"
          android:id="@+id/list"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

</RelativeLayout>

My mainactivity.java code is as follows:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "BlackBerry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    String[] values2 = new String[] {"good","bad","medium","not yet tested","don't kNow it","very cool","Just Windows","piece of shit","very cool","don't kNow it too"};
    ListView listView = (ListView) findViewById(R.id.listview);
    ListViewAdapter adapter1 = new ListViewAdapter (values,values2,android.R.id.list);
    listView.setAdapter(adapter1);
    }
}

My current custom adapter is as follows:

import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

String[] values;
String[] values2;
Context ctxt;
LayoutInflater layoutInflater;

public ListViewAdapter (String [] Values,String [] Values2, Context context) {
    values = Values;
    values2 = Values2;
    ctxt = context;
    layoutInflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return values.length;
}

@Override
public Object getItem(int position) {
    String [] res = new String [] {values [position],values2 [position]};
    return res;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView name1 = (TextView)convertView.findViewById(android.R.id.text1);
    TextView name2 = (TextView)convertView.findViewById(android.R.id.text2);
    name1.setText(values [position]);
    name2.setText(values2 [position]);
    return convertView;
}
}

The following error occurred while starting the application:

I want a description of this problem, not a link to the tutorial, because I tried to work through them, and this happened. I also read multiple stackoverflow threads, but none of them can solve my problem

Thank you for your help, alumni

Note: This is not the application I am developing. It is a different place where I will lose my new knowledge

resolvent:

There are many errors in your code. Let me point out

Error 1

Set adapter. Cannot be called without setcontentview()

 ListView listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(adapter1);

You should call

 setlistadapter(adapter1);

A better way is:

Use acticity instead of listactivity. Then do it in oncreate()

setConetentView(R.id.Your_main_View);
ListView listView = (ListView) findViewById(R.id.listview);
ListViewAdapter adapter1 = new ListViewAdapter (values,values2,this);
listView.setAdapter(adapter1);

Error 2

Your listviewadapter constructor has parameters

 (String[] Values,String [], Context)

But you're sending

 (values,values2,android.R.id.list)

You should send

 (values,values2,this) 

Error 3

You have not expanded the view in the dispatcher. If you do not expand the listview.xml view, you cannot convert it to a view type object, so you cannot use it to create a custom listview

What should you do

public static class Holder  //holder class containing view components/widgets
{

    TextView name1,name2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder(); 
View convertView=inflater.inflate(R.layout.row_list, null);
holder.name1 = convertView.(TextView)convertView.findViewById(R.id.text1);
holder.name2 = convertView.(TextView)convertView.findViewById(R.id.text2);
holder.name1.setText(values [position]);
holder.name2.setText(values2 [position]);
return convertView;
}

For a more detailed understanding, I wrote an example here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>