我是一名新秀 android 开发人员,在列表视图中膨胀 2 布局时面临一些问题我想要一个包含图像和文本视图的水平行作为单独的布局,然后在列表视图中调用这两个布局。我无法填充水平列表
我正在粘贴我的代码以供参考 我使用了 devsmart 水平视图库
package com.devsmart.demo;
import com.devsmart.android.ui.HorizontalListView;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class HorizontalListViewDemo extends Activity {
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
Log.d("app start ", "chal gya ");
ListView listview = (ListView) findViewById(R.id.listView1);
Log.d("find view ","chal gya ");
Vadapter adapter = new Vadapter(this, R.layout.row, dataObjects);
listview.setAdapter(adapter);
}
}
++++++++
package com.devsmart.demo;
import com.devsmart.android.ui.HorizontalListView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class Vadapter extends BaseAdapter{
Context context;
String[] data;
int resourceId;
Vadapter(Context context , int resourceId,String[] data ){
this.context = context;
this.data = data;
this.resourceId = resourceId;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView ;
if (convertView == null) {
//
rowView = LayoutInflater.from(context).inflate(R.layout.row, null);
} else {
rowView = convertView;
}
//
Log.d("start view in vertical ", "chal gya ");
HorizontalListView hListView = (HorizontalListView) rowView
.findViewById(R.id.listview);
Hadapter horListAdapter = new Hadapter(
context, R.layout.viewitem, data);
hListView.setAdapter(horListAdapter);
return rowView;
}
}
+++++
package com.devsmart.demo;
import java.util.concurrent.CountDownLatch;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class Hadapter extends BaseAdapter{
Context context;
String[] data;
int resourceId;
Hadapter(Context context ,int resourceId,String[] data){
Log.d("start objext in horizontal ", "chal gya ");
this.context = context;
this.data= data;
this.resourceId = resourceId;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("start view in horizontal ", "chal gya ");
View retval = LayoutInflater.from(context).inflate(this.resourceId, null);
TextView title = (TextView) retval.findViewById(R.id.title);
Button button = (Button) retval.findViewById(R.id.clickbutton);
title.setText(data[position]);
return retval;
}
}