ListView中的多个TextView?

时间:2014-07-06 14:33:39

标签: android listview

我有一个ListView,其中有两个TextView来自DataBase,但只显示一个row

MainActivity.java:

public class MainActivity extends ActionBarActivity {
public static final String DIR_SDCARD =Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE = DIR_SDCARD +"/Android/data/";
EditText editText;
DB db = new DB(MainActivity.this);
public String Titel_Drawer;
public String Titel_Drawert;
public String messageCursor;
public SQLiteDatabase  sql;
public Cursor cursor;
public Cursor cursort;
public ArrayList<String> array;
public ArrayList<String> arrayt;
public static String PACKAGE_NAME;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PACKAGE_NAME = getApplicationContext().getPackageName();
        File file= new File(DIR_DATABASE + PACKAGE_NAME + "/DB");
        file.mkdirs();
        db.GetPackageName(PACKAGE_NAME);
        db.CreateFile();
        try {
            db.CreateandOpenDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sql = db.openDataBase();
        cursor = sql.rawQuery("SELECT Comment FROM WebSite_MetaDataDBBack", null);
        cursort = sql.rawQuery("SELECT Title FROM WebSite_CategoryBack", null);
        array = new ArrayList<String>();
           if(cursor!=null)
            {
                if(cursor.moveToFirst())
                {
                    do {
                        Titel_Drawer = cursor.getString(cursor.getColumnIndex("Comment"));
                        array.add(Titel_Drawer);
                    } while (cursor.moveToNext());
                }
            }
           cursor.close();
           arrayt = new ArrayList<String>();
           if(cursort!=null)
            {
                if(cursort.moveToFirst())
                {
                    do {
                        Titel_Drawert = cursort.getString(cursort.getColumnIndex("Title"));
                        arrayt.add(Titel_Drawert);
                    } while (cursort.moveToNext());
                }
            }
           cursort.close();
        final List<Contact> listOfContact = new ArrayList<Contact>();
        listOfContact.add(new Contact(Titel_Drawert,Titel_Drawer));
        ContactAdapter ConAdapter = new  ContactAdapter(this,listOfContact);
        ListView list = (ListView) findViewById(R.id.mainListView);
        list.setAdapter(ConAdapter);
}
}

以下是我在Contact.java中的getset

public class Contact {
    private String title;
    private String description;

    public Contact(String title, String description) {
        super();
        this.title = title;
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

这是ContactAdapter.java:

public class ContactAdapter extends BaseAdapter implements OnClickListener {
    private Context context;

    private List<Contact> listContact;

    public ContactAdapter(Context context, List<Contact> listContact) {
        this.context = context;
        this.listContact = listContact;
    }
    public int getCount() {
        return listContact.size();
    }

    public Object getItem(int position) {
        return listContact.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        Contact entry = listContact.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.drawer_layout_main, null);
        }
        TextView tvContact = (TextView) convertView.findViewById(R.id.rowTextView_Main);
        tvContact.setText(entry.getTitle());

        TextView tvPhone = (TextView) convertView.findViewById(R.id.rowTextView_Main2);
        tvPhone.setText(entry.getDescription());
        return convertView;
    }

    @Override
    public void onClick(View view) {

    }
}

我的activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<ListView
        android:id="@+id/mainListView"
        android:layout_width="fill_parent"
        android:layout_height="300dp">
    </ListView> 
</LinearLayout>

我的drawer_layout_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/rowTextView_Main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

    <TextView
        android:id="@+id/rowTextView_Main2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的listOfContact只有1个项目:

final List<Contact> listOfContact = new ArrayList<Contact>();
listOfContact.add(new Contact(Titel_Drawert,Titel_Drawer));
ContactAdapter ConAdapter = new  ContactAdapter(this,listOfContact);

所以你的getCount()返回1。

public int getCount() {
    return listContact.size();
}

将适配器中的listContact替换为一个List,其中每个项目都有一个元素。