Populating ListView from a SQLite database
public class ConnectDatabase extends Activity {
private SimpleDBAdapter mDbHelper;
private ListView list;
private String[] values;
private String[] list1;
private int i=0;
/** Called when the activity is first created. */
@Override
public void onCreate(B开发者_运维百科undle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView )findViewById(R.id.simpleListView);
mDbHelper = new SimpleDBAdapter(ConnectDatabase.this);
mDbHelper.createDatabase();
mDbHelper.open();
values = mDbHelper.getEditTextValue();
mDbHelper.close();
for(i=0;i<10;i++) {
String tempvalue;
tempvalue=values[i];
list1[i]=tempvalue;
Log.v("log_tag"," tempvalue "+tempvalue);
Log.v("log_tag", "list1 is"+list1[i]);
//Log.v("log_tag"," list1"+list1[i]);
}
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values));
//list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
/*for(int i=0;i<values.length;i++) {
String templist=values[i];
list1[i]=templist;
}*/
String fname=values[position];
Log.v("log_tag", "The fname is"+ fname);
Intent intent=new Intent(ConnectDatabase.this, Userinfo.class);
intent.putExtra("FirstName",fname );
startActivity(intent);
}
});
// to remove the last comma
}
}
Here I am doing ListView
from SQLite database and want to display in ListView
with only the first 10 ListView rows at the time but I get the error at the line list1[i]=tempvalue;
so I can't copy array here anyone can help me to do copy the array name values where I get all the Column index values from my SQLite database.
Hope anyone have an idea to do this.
Your post is quite hard to read, but I think the issue is that list1
has not been initialised.
You need to add a line like this:
list1 = new String[10];
before you attempt to set a value within the array.
ps - I picked a value of 10
as your for
loop has i<10
.
精彩评论