我正在尝试构建一个菜单,其中应包含每个菜单项的标题和说明。我试图使用二维数组,我试图使用两个单独的数组。无法使其发挥作用。以下是我的最后一个代码。谢谢你的帮助。
我得到一个Exception指向这行代码和活动只是崩溃:
listView.setAdapter(channelNamesAdapter);
完整课程如下:
public class ChanelMenuActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ListView listView;
String[] channelNames = new String[] { "Title1", "Title2", "Title3" };
String[] channelDescriptions = new String[] { "Description1", "Description1", "Description1" };
ArrayAdapter channelNamesAdapter;
listView = (ListView) findViewById(R.id.menu_items);
channelNamesAdapter = new ArrayAdapter(this, R.layout.rowlayout, R.id.label, channelNames);
listView.setAdapter(channelNamesAdapter);
ArrayAdapter channelDescriptionAdapter;
listView = (ListView) findViewById(R.id.menu_items);
channelDescriptionAdapter = new ArrayAdapter(this,R.layout.rowlayout, R.id.description, channelDescriptions);
listView.setAdapter(channelDescriptionAdapter);
}
我的rowlayout.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:orientation="vertical" >
<ImageView
android:id="@+id/icon"
android:layout_width="173px"
android:layout_height="107px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="@drawable/thumb" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/description"
android:textSize="20px" >
</TextView>
</LinearLayout>
答案 0 :(得分:0)
1)创建自己的类,其中包含channelName
和channelDescription
属性,使用getter和setter
2)扩展BaseAdapter
3)使用ArrayList<yourClass>
作为适配器数据
编辑示例:
public class MyClass{
private String channelName, channelDescription;
public MyClass(){
/* default constructor */
}
public void setName(String channelName){
this.channelName = channelName;
}
public void setDesc(String channelDescription){
this.channelName = channelDescription;
}
public String getName(){
return this.channelName;
}
public String getDesc(){
return this.channelDescription;
}
}
在Activity
:
ArrayList<MyClass> data = new ArrayList<MyClass>();
for(int i=0;i<10;i++){
MyClass mc = new MyClass();
mc.setName("Name " + i);
mc.setDesc("Desc " + i);
data.add(mc);
}
myListView.setAdapter(new MyAdapter(this, data));
MyAdapter类:
public class MyAdapter extends BaseAdapter {
private ArrayList<MyClass> data;
private LayoutInflater inflater;
private ViewHolder holder;
static class ViewHolder {
TextView tvName, tvDesc;
}
public MyAdapter(ArrayList<MyClass> data, Context c) {
this.data = data;
inflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyClass item = (MyClass) getItem(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.your_list_row, null);
holder.tvName = (TextView) convertView
.findViewById(R.id.tvItemName);
holder.tvDesc = (TextView) convertView
.findViewById(R.id.tvItemDesc);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(item.getName());
holder.tvDesc.setText(item.getDesc());
return convertView;
}
}
答案 1 :(得分:0)
经过一番努力和许多教程后,我终于成功了。这就是我如何实现我所需要的。非常好的菜单列表。每个菜单项都有3个对象:图像,标题和描述。 OnClickListener也就位。点击菜单项将启动Toast。所有菜单对象的数据都来自数组。
<强> MainActivity.java 强>
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent channelIntent = new Intent(this, ChanelMenuActivity.class);
//i.putExtra("value1", result.toString());
startActivity (channelIntent);
}
}
<强> ChanelMenuActivity.java 强>
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class ChanelMenuActivity extends ListActivity {
Level data[] = null;
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
ListView lv1=(ListView)findViewById(R.id.listViewIgor);
final Level data[] = new Level[]
{
new Level(R.drawable.icon, "Heading 1", "Description 1"),
new Level(R.drawable.thumb, "Heading 2", "Description 2"),
new Level(R.drawable.mplayer, "Heading 3", "Description 3"),
new Level(R.drawable.thumb, "Heading 4", "Description 4"),
new Level(R.drawable.mplayer, "Heading 5", "Description 5"),
new Level(R.drawable.icon, "Heading 6", "Description 6"),
new Level(R.drawable.thumb, "Heading 7", "Description 7"),
new Level(R.drawable.mplayer, "Heading 8", "Description 8"),
new Level(R.drawable.icon, "Heading 9", "Description 9"),
new Level(R.drawable.thumb, "Heading 10", "Description 9"),
new Level(R.drawable.mplayer, "Heading 11", "Description 10"),
new Level(R.drawable.icon, "Heading 12", "Description 11"),
new Level(R.drawable.thumb, "Heading 13", "Description 12"),
new Level(R.drawable.mplayer, "Heading 14", "Description 13"),
new Level(R.drawable.icon, "Heading 15", "Description 14")
};
LevelAdapter adp=new LevelAdapter(this, R.layout.list_item, data);
lv1.setAdapter(adp);
lv1.setOnItemClickListener(
new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id){
System.out.println("Igor's Click Was heard");
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
Level channelData = data[position];
CharSequence text = channelData.title;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
toast.show();
onCreate(icicle);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menu){
//This one will respond to More Options menu click at the top right
Toast.makeText(this, "item" + " selected", Toast.LENGTH_LONG).show();
return true;
}
}
<强> Level.java 强>
public class Level {
public int icon;
public String title;
public String title2;
public Level()
{
super();
}
public Level(int icon, String title, String title2) {
super();
this.icon = icon;
this.title = title;
this.title2 = title2;
}
}
<强> LevelAdapter.java 强>
import android.app.Activity;
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 LevelAdapter extends ArrayAdapter<Level> {
static Context context;
static int layoutResourceId;
Level data[] = null;
public LevelAdapter(Context context, int layoutResourceId, Level[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ChannelDataHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//row.setMinimumHeight(200);
holder = new ChannelDataHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTitle2 = (TextView)row.findViewById(R.id.txtTitle2);
row.setTag(holder);
}
else
{
holder = (ChannelDataHolder)row.getTag();
}
Level channelData = data[position];
holder.imgIcon.setImageResource(channelData.icon);
holder.txtTitle.setText(channelData.title);
holder.txtTitle2.setText(channelData.title2);
return row;
}
static class ChannelDataHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtTitle2;
}
}
<强> list_item.xml 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/imgIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="173px"
android:layout_height="107px"
android:layout_marginLeft="1px"
android:layout_marginRight="10px"
android:layout_marginTop="4px" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imgIcon"
android:layout_toRightOf="@+id/imgIcon" />
<TextView
android:id="@+id/txtTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtTitle"
android:layout_below="@+id/txtTitle" />
</RelativeLayout>
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="@+id/listViewIgor"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:dividerHeight="1dp" />
</RelativeLayout>
<强>的AndroidManifest.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tv.emoe.www"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ChanelMenuActivity"></activity>
<activity android:name="LevelAdapter"></activity>
<activity android:name="Level"></activity>
</application>
</manifest>