我对android studio中的编程非常陌生,并希望创建一个我可以在工作中使用的应用程序。基本思想是从CSV文件将产品列表导入应用程序并将其显示为列表视图,以便为每个文件显示相关文档。
程序从CSV文件中读取没有任何问题但是当我尝试使用适配器查看ListView中的数据时,适配器始终显示“无法解析构造函数”。我一直在寻找答案,但我放弃了。任何人都可以解释一下吗?
MainActivity.java
package com.example.mblapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import static com.example.mblapplication.R.id.ProdList1;
public class MainActivity extends AppCompatActivity {
public List<ProdSample> productSamples = new ArrayList<>();
public void readProdData() {
InputStream is = getResources().openRawResource(R.raw.products);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
String line = "";
try {
// Step over headers
reader.readLine();
while ((line = reader.readLine()) != null) {
Log.d("MBLApplication", "Line is: " + line);
// Split by ","
String[] tokens = line.split(",");
// Read the data
ProdSample sample = new ProdSample();
if (tokens[0].length() > 0) {
sample.setSAP(tokens[0]);
} else {
sample.setSAP("");
}
sample.setProdID(tokens[1]);
sample.setWidth(tokens[2]);
sample.setLooms(tokens[3]);
productSamples.add(sample);
Log.d("MBLApplication", "Just created: " + sample);
}
} catch (IOException e) {
Log.wtf("MBLApplication, Error reading file on line " + line, e);
e.printStackTrace();
}
}
ListView listView;
//List<String> listItems = new ArrayList<>(5);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readProdData();
ArrayAdapter<String> productAdapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_list_item_1, ProdList1, productSamples); //ERROR HERE
//listItems.add(0, "Hello");
//listItems.add(1, "World");
ListView listView = (ListView) findViewById(R.id.ProdList1);
listView.setAdapter(productAdapter);
}
}
ProdSample.class
package com.example.mblapplication;
class ProdSample {
private String SAP;
private String ProdID;
private String Width;
private String Looms;
public String getSAP() {
return SAP;
}
public void setSAP(String SAP) {
this.SAP = SAP;
}
public String getProdID() {
return ProdID;
}
public void setProdID(String prodID) {
this.ProdID = prodID;
}
public String getWidth() {
return Width;
}
public void setWidth(String width) {
this.Width = width;
}
public String getLooms() {
return Looms;
}
public void setLooms(String looms) {
Looms = looms;
}
@Override
public String toString() {
return "ProdSample{" +
"SAP='" + SAP + '\'' +
", ProdID=" + ProdID +
", Width=" + Width +
", Looms='" + Looms + '\'' +
'}';
}
}
答案 0 :(得分:0)
当你使用android默认布局为你的列表项android.R.layout.simple_list_item_1时,你不需要下一个参数ProdList1。只需删除它
CustomArrayAdapter<ProdSample> productAdapter = new CustomArrayAdapter<ProdSample>
(MainActivity.this, android.R.layout.simple_list_item_1, productSamples);
否则,如果您使用了自己的列表项布局,那么
CustomArrayAdapter<ProdSample> productAdapter = new CustomArrayAdapter<ProdSample>
(MainActivity.this, R.id.your_textview_id_in_your_list_item_layout, productSamples);
然后你应该列出项目布局xml文件
// your_list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/your_textview_id_in_your_list_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
您的自定义数组适配器
public class CustomArrayAdapter extends ArrayAdapter<ProdSample> {
private static class ViewHolder {
private TextView itemView;
}
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<ProdSample> items) {
super(context, textViewResourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.your_list_item_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.itemView = (TextView) convertView.findViewById(R.id.your_textview_id_in_your_list_item_layout);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ProdSample sample = getItem(position);
if (item!= null) {
viewHolder.itemView.setText(sample.toString());
}
return convertView;
}
}