我想在我的自定义布局中创建多个列表视图。我为布局提供了一个 ID:@+id/listview
我想将其作为ListView
布局附加并调用。我R.id.listview
在MobileArrayAdapter.java
文件中使用它来调用它。文件中没有错误,但仍然无法编译。它给出了一个致命的例外。基本上,我想将数据从 excel 文件导入到嵌入在相对布局中的多个列表视图中。有人可以提供任何示例或帮助我删除此代码的运行时错误!谢谢...
我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_horizontal_margin"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kollege_name"
android:width="170dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:text="@string/grade_value"
android:width="120dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:text="@string/code_value" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/your_fit"
android:textSize="12sp" />
<RelativeLayout
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/stream_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/stream_name"
android:layout_marginTop="3dp" />
<ImageView
android:id="@+id/your_fit_image"
android:layout_width="17sp"
android:layout_height="17sp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/stream_name"
android:layout_marginRight="5dp"
android:contentDescription="@string/desc"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
主java文件:
package org.in.pappu;
import org.in.pappu.adaptor.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class Details_Kollege extends ListActivity {
static final String[] streams_list = new String[] { "EXTC", "IT", "COMP", "MECH", "INST"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MobileArrayAdapter (this, streams_list));
}
/*
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.base, menu);
return true;
}
*/
}
MobileArrayAdapter java
package org.in.pappu.adaptor;
import org.in.pappu.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.id.listview, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.id.listview, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.stream_name);
ImageView imageView = (ImageView) rowView.findViewById(R.id.your_fit_image);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("EXTC")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageResource(R.drawable.ic_launcher);
}
return rowView;
}
}