Android 如何在ListView中从SD卡显示TexView和ImageView
我真的很感激你能帮我解答这个问题。 我正在构建的应用程序是从一个网址下载文本和图像(使用JSON),并将文本存储在sqlite数据库中,图像存储在手机的SD卡中。 然后我想在listView中显示文本和关联的图像。 我在显示文本时没有问题,但无法显示imageView 下面是MainActivity中的代码,它从sqlite数据库中提取文本并填充列表字符串Android 如何在ListView中从SD卡显示TexView和ImageView,android,sqlite,listview,textview,imageview,Android,Sqlite,Listview,Textview,Imageview,我真的很感激你能帮我解答这个问题。 我正在构建的应用程序是从一个网址下载文本和图像(使用JSON),并将文本存储在sqlite数据库中,图像存储在手机的SD卡中。 然后我想在listView中显示文本和关联的图像。 我在显示文本时没有问题,但无法显示imageView 下面是MainActivity中的代码,它从sqlite数据库中提取文本并填充列表字符串 @SuppressLint("NewApi") private void displayListView() { IgroDataba
@SuppressLint("NewApi") private void displayListView() {
IgroDatabaseHelper helper = new IgroDatabaseHelper(getBaseContext());
globalVariable.userLang = helper.userLanguage();
ArticlesTable = globalVariable.userLang + "Articles";
numRows = helper.NumEntries(ArticlesTable);
int i = 1;
String headText;
String artID;
String Date;
File imgFile;
List<String> headingArray = new ArrayList<String>(); // array holding the headings to show in TextView
List<String> artIDArray = new ArrayList<String>(); // array of article IDs used to get the name of the corresponding image
List<String> imgArray = new ArrayList<String>();
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
lv = (ListView) findViewById(R.id.list);
String filename;
while (i <= numRows){ //loop over the number of Articles headings
headText = helper.getTextFromTable(ArticlesTable, i, "heading");
artID = helper.getTextFromTable(ArticlesTable, i, "artID");
image = Environment.getExternalStorageDirectory() + artID + ".png”;
// getting the image to be shown
filename= artID + ".png";
imgFile = new File(SDCardRoot,filename);
Bitmap b = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
headingArray.add(headText);
artIDArray.add(artID);
imgArray.add(filename);
i= i + 1;
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.article_row, R.id.heading, headingArray);
lv.setAdapter(arrayAdapter);
}
}
@SuppressLint(“NewApi”)私有void displayListView(){
IgroDatabaseHelper=新的IgroDatabaseHelper(getBaseContext());
globalVariable.userLang=helper.userLanguage();
ArticlesTable=globalVariable.userLang+“Articles”;
numRows=helper.NumEntries(ArticlesTable);
int i=1;
字符串头文本;
弦乐艺术;
字符串日期;
文件imgFile;
List headingArray=new ArrayList();//包含要在TextView中显示的标题的数组
List artIDArray=new ArrayList();//用于获取相应图像名称的项目ID数组
List imgArray=new ArrayList();
文件SDCardRoot=Environment.getExternalStorageDirectory().getAbsoluteFile();
lv=(ListView)findViewById(R.id.list);
字符串文件名;
while(i您可以直接显示来自服务器的图像而不保存它们。但是,如果您想这样做,您应该尝试使用此方法列出SD卡中的图像
读一读,这是一个好主意tutorial@tianyu谢谢。另一位成员也推荐了同样的教程。我做了大量的研究,以前没有找到过,所以谢谢你给我指出。我会让你知道我的进展。@tianyu我有一个问题。现在我循环表中的行数来显示文本,我还记得吗执行此操作并在while循环中调用Loader函数?我不清楚如何以这种方式使用它。您知道吗?感谢您的代码,您可以在BitmapFactory.decodeFile()之后调用smallimage.setImageBitmap()
来设置图像。最好在AsyncTask.doInBackground(…)中执行decodeFile()
。如果您正在使用本教程,您可能需要根据文件名获取每个图像的“标题”信息。谢谢,我该如何将其添加到matrixCursor?感谢您提供的链接。我将以自己的方式阅读本教程,并尝试使其适应我的需要。例如,如何从sqlite数据库添加标题。我将获得ba我希望,这是一个成功的故事!再次感谢。你能告诉我我是否可以将代码放入while循环中的链接中吗?我如何循环表格中的行以获取文本?非常感谢。将你正在使用的代码发送给我,以便我能帮你谢谢你。你太好了。你想让我将我的主要活动发送给你吗java和我拥有的2个XML文件?我怎样才能将其发送给您?谢谢!您是否可以将您的电子邮件地址或其他方式发送给我以向您发送代码?再次,非常感谢!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background = "#5a9b3e"
android:alpha = "0.7"
>
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:alpha = "1.0"
android:layout_marginStart="10sp"
android:layout_marginLeft="10sp"
android:layout_weight="5" />
<ImageView
android:id="@+id/smallimage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"/>
</LinearLayout>