【问题标题】:How to add ImageView, TextView and Button in ListView in android如何在android的ListView中添加ImageView、TextView和Button
【发布时间】:2012-09-14 07:34:50
【问题描述】:

在我的应用程序中,我试图创建一个包含 ImageView、TextView 和 Button 的 ListView。我创建了一个单独的 XML 文件并在该 XML 和我的主 java 文件中拖动所有上述元素我创建了 BaseAdapter 的对象和在 getView() 方法中我已经声明了这些元素但是当我运行应用程序时我不能见清单。我没有使用 BaseAdapter,所以我缺少一些代码来查看列表。我还想在按钮上应用一些操作,所以也请告诉我该代码。

我的 main.xml 文件的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

</LinearLayout>

我的 list_item.xml 的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="15dp"
        android:text="Medium Text"
        android:textSize="15dp" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="25dp"
        android:text="Button" />

</LinearLayout>

我的 main.java 文件的代码:

public class CustomListActivity extends Activity {
    /** Called when the activity is first created. */
    ListView lv;
    LayoutInflater inflator;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv = (ListView)findViewById(R.id.listView1);

        BaseAdapter bs = new BaseAdapter() {

            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View vw = inflator.inflate(R.layout.list_items, null);
                ImageView img = (ImageView)findViewById(R.id.imageView1);
                TextView tv = (TextView)findViewById(R.id.textView1);
                Button btn = (Button)findViewById(R.id.button1);
                return vw;
            }

            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }

            public int getCount() {
                // TODO Auto-generated method stub
                return 2;
            }
        };
    }
}

提前谢谢....

【问题讨论】:

  • 请看this example;在类似的情况下帮助了我很多

标签: android android-listview baseadapter


【解决方案1】:

好的,

    public class App_Adapter extends BaseAdapter implements OnClickListener{
      private  Activity mActivity;
      private  List<App_List> mList;
      private static LayoutInflater inflater=null;
     private PackageManager pm;
     private String appclass;
private  ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
    // TODO Auto-generated constructor stub
    this.mActivity= favouriteApp;
    this.mList= mAppList;
    this.appclass = appclass;
    inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm=mActivity.getPackageManager();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mList.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View mView=convertView;
    if (convertView == null)  
    mView = inflater.inflate(R.layout.app_adapter, parent,false);
    App_List mAppList= mList.get(position);
   **here i am setting two textview and one button**
    ((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
    ((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
    boolean status = isAppInstalled(mAppList.getApp_Pkg());
    Button btn = (Button)  mView.findViewById(R.id.button_appStatus);
    **// register the button for clicklistener**
    btn.setOnClickListener(this);

return mView;
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


}

并从您的活动中调用此适配器类。

【讨论】:

  • 你能再告诉我一件事吗?如何在您的代码中应用 onTextChangedListener() ?因为 onTextChangedListener() 需要 ArrayAdapter 但上面的代码没有任何 ArrayAdapter.. 您的帮助将不胜感激.. 谢谢。
【解决方案2】:

你肯定错过了lv.setAdapter(bs);

【讨论】:

  • 写上面的代码给出了一个 java.lang.NullPointerException.. 我正在写 lv.setAdapter(bs);我的 baseAdapter 结束的地方。即在}之后; lv.setAdapter(bs);请纠正我..谢谢..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多