创建ListView图标和文本Android时动态数组而不是静态

时间:2010-10-29 05:44:46

标签: android database arrays listview dynamic

我正在使用以下代码创建一个带有文本和图标的ListView,但我遇到的问题是以下我希望数组是动态的而不是静态的,所以我的列表是动态创建的而不是统计的。我的最终目标是从我的Db中检索特定的字符串,然后在Listview中显示它,每个字符串都在每行中。 非常感谢你提前。

我的源代码:

    import java.util.ArrayList;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class History extends ListActivity implements OnItemClickListener {

String keyWord = "";
String histDesRes = "";
String history = "";
String rawImage;
ArrayList<String> Ra = new ArrayList<String>();
SQLiteDatabase mydb;
TextView selection;
public String[] result={keyWord, "ipsum", "dolor",
        "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history);
    populate();




}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //populate();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //populate();
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    //populate();
}

void populate() {

    mydb = openOrCreateDatabase("vivo_id.db",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);
    Cursor c = mydb.query("VIVOID", null, null, null, null, null, null);
    // start the managing cursor
    startManagingCursor(c);
    // move to first row
    c.moveToFirst();
    // continue searching until it is the last row in the column
    while (c.isAfterLast() == false) {

        rawImage = c.getString(c.getColumnIndex("couponThumbnailResult"));
        keyWord = c.getString(c.getColumnIndex("keyWordResult"));
        histDesRes = c.getString(c.getColumnIndex("historyDescriptionResult"));
        //Toast.makeText(getApplicationContext(),
                //history, Toast.LENGTH_LONG).show();
        history = keyWord+" "+ histDesRes;
        c.moveToNext();
        }

    // close everything
    c.close();
    mydb.close();
    setListAdapter(new IconicAdapter());
    selection=(TextView)findViewById(R.id.selection);

}

public void onListItemClick(ListView parent, View v, int position, long id) {
    Toast.makeText(getApplicationContext(),
            "Inside listen", Toast.LENGTH_LONG).show();

}

class IconicAdapter extends ArrayAdapter {
    IconicAdapter() {
        super(History.this, R.layout.row, result);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.label);
        label.setText(result[position]);

        ImageView icon = (ImageView) row.findViewById(R.id.icon);


            icon.setImageResource(R.drawable.ok);

        return (row);
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}

    }

    /**
     * On Item click event for the list view
     */

1 个答案:

答案 0 :(得分:0)

    while (c.isAfterLast() == false) {
       rawImage = c.getString(c.getColumnIndex("couponThumbnailResult"));
       keyWord = c.getString(c.getColumnIndex("keyWordResult"));
       histDesRes = c.getString(c.getColumnIndex("historyDescriptionResult"));
       //Toast.makeText(getApplicationContext(),
       //history, Toast.LENGTH_LONG).show();
       history = keyWord+" "+ histDesRes;
       //Use an ArrayList to store your results from the DB
       c.moveToNext();
    }

 String[] results = yourArrayList.toArray();

在适配器中使用此数组....