views:

66

answers:

1

Hi,

I am using this following code which creates a ListView with text and an icon but the problem that I have which the following I want the array to be dynamic instead of static so my list gets created dynamically not statistically. My ultimate goal is to retrieve specific strings from my Db and to display it then on the Listview where every string would be in each row. Thank you very much in advance.

My source code:

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 */

A: 

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();

  use this array in your adapter....
Umesh
Hi Umesh,First of all i just wanted to thank you for taking the time to answer but unfortunately this solution did not work for me if you can please give a little more explicit explanation and maybe post some believe or not this a matter of life or death it's so vital to my app thank you very much.
MR Mido
do you get an error or something. In that case can you post the log.
Umesh