求助 Android ListView ImageButton显示

zxh_2012 2013-08-26 09:21:37
问题如图片所示,跪求大神解救!

下面是各个文件,估计是布局有问题,再次拜求大神解救!
.xml
<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="match_parent"
android:background="#000"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">

<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageButton
android:id="@+id/ibtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"/>
</LinearLayout>

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java

package com.example.androidlistv4;

import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

private List<Map<String, Object>> mdata;
private MyAdapter madapter;
private ListView lstv;

private static Activity _instance;

public static Activity getInstance() {
return _instance;
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

_instance = this;

mdata = MyData.getData();
madapter = new MyAdapter(this);

lstv = (ListView)findViewById(R.id.lv);
lstv.setAdapter(madapter);
lstv.setOnItemClickListener(
new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
// Toast.makeText(getBaseContext(), "ListitemClicked!!!", 1000).show();
MyDialog.showInfo();
}
});

}

}

MyAdapter.java

package com.example.androidlistv4;

import com.example.androidlistv4.ViewHolder;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;

public class MyAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private Context context;



public MyAdapter(Context mcontext)
{
// TODO Auto-generated constructor stub
this.mInflater = LayoutInflater.from(mcontext);
context = mcontext;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return MyData.getData().size();// 相当于mData.size();
}

@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)
{
// TODO Auto-generated method stub

ViewHolder holder = null;
if (convertview == null) {
holder = new ViewHolder();
convertview = mInflater.inflate(R.layout.activity_main, null);

holder.imgv = (ImageView) convertview.findViewById(R.id.iv);
holder.imgb = (ImageButton) convertview.findViewById(R.id.ibtn);

convertview.setTag(holder);
} else {
holder = (ViewHolder) convertview.getTag();
}

holder.imgv.setBackgroundResource((Integer) MyData.getData()
.get(position).get("song"));
holder.imgb.setBackgroundResource((Integer) MyData.getData()
.get(position).get("bofang"));

holder.imgb.setOnClickListener(new ImageView.OnClickListener()
{
public void onClick(View v){
// Toast.makeText(context, "Imgbtn Clicked!!!", 1000).show();
MyDialog.showInfo();
}
});

holder.imgv.setOnClickListener(new ImageView.OnClickListener()
{
public void onClick(View v){
// Toast.makeText(context, "ImgView Clicked!!!", 1000).show();
MyDialog.showInfo();
}
});

return convertview;
}



}

MyData.java

package com.example.androidlistv4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyData {

public MyData() {
// TODO Auto-generated constructor stub
}

public static List<Map<String, Object>> getData()
{
List<Map<String, Object>> mlst = new ArrayList<Map<String,Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("song", R.drawable.music);
map1.put("bofang", R.drawable.play);

Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("song", R.drawable.music);
map2.put("bofang", R.drawable.play);

Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("song", R.drawable.music);
map3.put("bofang", R.drawable.play);

Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("song", R.drawable.music);
map4.put("bofang", R.drawable.play);

mlst.add(map1);
mlst.add(map2);
mlst.add(map3);
mlst.add(map4);

return mlst;

}
}

MyHolder.java

package com.example.androidlistv4;

import android.widget.ImageButton;
import android.widget.ImageView;

public class ViewHolder {

public ImageView imgv;
public ImageButton imgb;

}

...全文
49 点赞 收藏 3
写回复
3 条回复
zxh_2012 2013年08月28日
二楼正解,其实我已经解决了,分开布局就行!多谢各位指点!
回复 点赞
Jenly_Yu 2013年08月27日
定义两个xml文件: 把 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:descendantFocusability="blocksDescendants"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageButton android:id="@+id/ibtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="300dp"/> </LinearLayout> 与 <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="match_parent" /> 分做两个xml就可以了、
回复 点赞
LichKingSZ 2013年08月27日
太长了不全看了,不过你自己这样写的代码当然会显示出来。。。你这个imageButton是想放到item里面?
<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="match_parent"
    android:background="#000"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:descendantFocusability="blocksDescendants">
回复 点赞
发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告