创建ListView图标和文本时动态数组而不是静态Android
我正在使用下面的代码创建一个带有文本和图标的 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
*/
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
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的适配器中使用这个数组......
use this array in your adapter....