当前位置:编程学习 > wap >>

求助 Android ListView ImageButton显示

问题如图片所示,跪求大神解救!

下面是各个文件,估计是布局有问题,再次拜求大神解救!
.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;

}

android listview 布局 imageview 图片 class --------------------编程问答-------------------- 太长了不全看了,不过你自己这样写的代码当然会显示出来。。。你这个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">
--------------------编程问答--------------------
定义两个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就可以了、 --------------------编程问答-------------------- 二楼正解,其实我已经解决了,分开布局就行!多谢各位指点!
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,