我正在尝试为我的listview创建一个适配器,我在getview()上遇到错误,特别是在View itemView = inflater.inflate(R.layout.activity_results,parent,false)上;感谢。
错误表示无法解析activity_results或不是字段
public class testadapter extends BaseAdapter
{
Context context;
String[] question;
String[] answer;
LayoutInflater inflater;
public testadapter(Context context, String[] question, String[] answer) {
this.context = context;
this.question = question;
this.answer = answer;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return question.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
public TextView txtViewTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtquestion;
TextView txtanswer;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Locate the TextViews in listview_item.xml
View itemView = inflater.inflate(R.layout.activity_results, parent, false);
txtquestion = (TextView) itemView.findViewById(R.id.text1);
txtanswer = (TextView) itemView.findViewById(R.id.text2);
// Capture position and set to the TextViews
txtquestion.setText(question[position]);
txtanswer.setText(answer[position]);
return itemView;
}
}
答案 0 :(得分:0)
检查您运行代码的分辨率的特定布局文件夹中是否存在activity_results.xml
。可能需要将此xml复制到其他布局文件夹中。
您需要检查Supporting Different Screens in Android
- http://developer.android.com/training/multiscreen/index.htmlhttp://developer.android.com/training/multiscreen/index.html
答案 1 :(得分:0)
重新生成R文件可能会解决问题。
转到“项目”菜单,查看是否选中“自动构建”选项。取消选中它。 在文件资源管理器中右键单击项目,然后选择“构建”
答案 2 :(得分:0)
// try this way hope this demo help you....
1. Main Xml file for holding "ListView" (activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lstMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
2. ListItem Xml file for holding "ListView" Items (list_item.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtQns"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtAns"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
3. Activity (MainActivity)
public class MainActivity extends Activity {
private ListView lstMain;
private MyCustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstMain = (ListView) findViewById(R.id.lstMain);
String[] questionArray = new String[]{"Qun-1","Qun-2","Qun-3","Qun-4","Qun-5"};
String[] answerArray = new String[]{"Ans-1","Ans-2","Ans-3","Ans-4","Ans-5"};
// set adapter dara
adapter = new MyCustomAdapter(this,questionArray,answerArray);
lstMain.setAdapter(adapter);
lstMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
try{
Toast.makeText(MainActivity.this,adapter.getItem(pos).toString(),Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
}
});
}
class MyCustomAdapter extends BaseAdapter
{
Context context;
String[] question;
String[] answer;
public MyCustomAdapter(Context context, String[] question, String[] answer) {
this.context = context;
this.question = question;
this.answer = answer;
}
@Override
public int getCount() {
return question.length;
}
@Override
public Object getItem(int position) {
return question[position]+"\n"+answer[position];
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
holder.txtQuestion = (TextView) convertView.findViewById(R.id.txtQns);
holder.txtAnswer = (TextView) convertView.findViewById(R.id.txtAns);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
// Capture position and set to the TextViews
holder.txtQuestion.setText(question[position]);
holder.txtAnswer.setText(answer[position]);
return convertView;
}
}
public static class ViewHolder
{
public TextView txtQuestion;
public TextView txtAnswer;
}
}