在listview中,所有值都插入到第一个位置

时间:2016-01-01 09:10:49

标签: java android listview android-listview

以下是我的java类

string s的值来自数据库缓冲区, 我希望每当值到来时,我将作为第一个可点击元素存储在listview中。 但是所发生的是所有值都作为项目1存储在列表视图的第一个位置

             import android.database.Cursor;
             import android.os.Bundle;
              import android.support.v7.app.AppCompatActivity;
              import android.widget.ArrayAdapter;
              import android.widget.ListView;
              import android.widget.TextView;

              import java.util.ArrayList;
               import java.util.Arrays;


      public class HistoryActivity extends AppCompatActivity {

 ListView lv;
String s= null;
TextView tv1;
DatabaseHelper mydb;
StringBuffer buffer;
private ArrayAdapter<String> listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acticity_history);
    lv = (ListView) findViewById(R.id.listView);
    mydb = new DatabaseHelper(this);

    Cursor res = mydb.getAllData();
    if(res.getCount() == 0)
    {
        return;

    }

    buffer = new StringBuffer();
    while(res.moveToNext()){
        buffer.append(""+ res.getString(1)+ "\n");
    }
    s = buffer.toString();

    String[] values = new String[]{s};


    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(values) );

    listAdapter = new ArrayAdapter<String>(this, R.layout.simpleraw, planetList);

    lv.setAdapter(listAdapter);} }     

下面是listview的xml布局

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center_horizontal"
    android:background="#000000"/>
</LinearLayout>

下面是textview的simpleraw布局文件

      <?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textColor="#FFFFFF"
    android:textSize="16sp" >
</TextView>

2 个答案:

答案 0 :(得分:2)

您的所有值都会附加到Redirect::to();。用以下内容替换您的代码,

String s

将OnItemClickListener设置为listView,

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acticity_history);
lv = (ListView) findViewById(R.id.listView);
mydb = new DatabaseHelper(this);

Cursor res = mydb.getAllData();
if(res.getCount() == 0)
{
    return;
}

else
{
  ArrayList<String> planetList = new ArrayList<String>();
  while(res.moveToNext()){
    planetList.add(res.getString(1));
    }

    listAdapter = new ArrayAdapter<String>(this, R.layout.simpleraw, planetList);

   lv.setAdapter(listAdapter);
  }
 } 
} 

答案 1 :(得分:0)

您正在将数据库的字符串附加到stringbuffer和 以下代码不会为您提供数组中的所有项目。该数组只有一个项目。

String[] values = new String[]{s};

请使用以下代码。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_history);
        lv = (ListView) findViewById(R.id.listView);
        mydb = new DatabaseHelper(this);

        Cursor res = mydb.getAllData();
        if(res.getCount() == 0)
        {
            return;

        }

        ArrayList<String> planetList = new ArrayList<String>();
        while(res.moveToNext()){
            planetList.add( res.getString(1));
        }

        listAdapter = new ArrayAdapter<String>(this, R.layout.simpleraw, planetList);

   }