Android:自定义列表视图,适配器中的表格视图

Posted

技术标签:

【中文标题】Android:自定义列表视图,适配器中的表格视图【英文标题】:Android: Customize list view, Table view in adapter 【发布时间】:2014-05-02 20:06:55 【问题描述】:

我以编程方式在列表视图适配器中创建了一个表视图。为此,我首先创建了一个适配器布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="@android:color/black"
    android:stretchColumns="*"
    android:id="@+id/tablelayout" >
</ableLayout>

那么我在适配器中所做的是:

public class DemoAdapter extends ArrayAdapter<String> 
    //Global Variable declaration
    Context myContext;
    String[] key, value, loop;

    public DemoAdapter(Context context, String[] key, String[] value, String[] loop)
        super(context, R.layout.demo_screen_adapter, loop);
        this.myContext = context;
        this.key = key;
        this.value = value;
        this.loop = loop;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
        View row = convertView;
        if (row == null)
            // get reference to the activity
            LayoutInflater inflater = ((Activity) myContext).getLayoutInflater();
            // Inflate the custom text which will replace the default text view
            //list_item is a simple linear layout which contain textView1 in it. 
            row = inflater.inflate(R.layout.demo_screen_adapter, parent, false);
        

        TableLayout tableLayout = (TableLayout) row.findViewById(R.id.tablelayout);
        for(int i=0; i<5; i++)
            TableRow tableRow = new TableRow(myContext);
            tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

            TextView tvKey = new TextView(myContext);
            tvKey.setText(key[(position*5)+i]);
            tvKey.setTextColor(Color.WHITE);
            tvKey.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            tableRow.addView(tvKey);

            TextView tvValue = new TextView(myContext);
            tvValue.setText(value[(position*5)+i]);
            tvValue.setTextColor(Color.WHITE);
            tvValue.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            tableRow.addView(tvValue);
            tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        
        return row;
    

这很好用。在我的每个列表项中,都有一个包含大约两列和五行的表格视图。

现在问题是:

我想在单击列表项时获取特定值。我不能这样做。 我尝试的是:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
    Toast.makeText(this, ""+(String) arg0.getItemAtPosition(position), Toast.LENGTH_LONG).show();

但它显示了我点击的位置。

我该怎么做,请指导我。

【问题讨论】:

【参考方案1】:

在您的自定义适配器中。使用支架。

这是我的自定义适配器

import java.util.ArrayList;

import com.samplelogin.CustomerDetails;
import com.samplelogin.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class UserCustomAdapter extends ArrayAdapter<Customers> 
    Context context;
    int layoutResourceId;
    ArrayList<Customers> data = new ArrayList<Customers>();

    public UserCustomAdapter(Context context, int layoutResourceId,
            ArrayList<Customers> data) 
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        View row = convertView;
        UserHolder holder = null;

        if (row == null) 
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new UserHolder();
            holder.textName = (TextView) row.findViewById(R.id.textView1);
            holder.textAddress = (TextView) row.findViewById(R.id.textView2);
            holder.textLocation = (TextView) row.findViewById(R.id.textView3);
            holder.btnEdit = (Button) row.findViewById(R.id.button1);

            row.setTag(holder);
         else 
            holder = (UserHolder) row.getTag();
        
        Customers user = data.get(position);        
        holder.textName.setText(user.getName());
        holder.textAddress.setText(user.getAddress());
        holder.textLocation.setText("");

        holder.btnEdit.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View v) 
                // TODO Auto-generated method stub
                Customers user = data.get(position);
                user.setTID(user.getID());
                user.setTName(user.getName());
                user.setTAddress(user.getAddress());
                user.setTTelNo(user.getTelNo());
                user.setTMobileNo(user.getMobileNo());
                user.setTFaxNo(user.getFaxNo());

                Intent intent = new Intent(context, CustomerDetails.class);
                context.startActivity(intent);
            
        );
        return row;

    

    static class UserHolder 
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        Button btnEdit;
        Button btnDelete;
    


在我的活动中。我把数据放在上面:

声明变量

 // Customer
   ListView CustomerList;
    UserCustomAdapter CustomerAdapter;
    ArrayList<Customers> CustomerArray = new ArrayList<Customers>();
    Button addCustomer;

用数据填充你的值。

        // Customer Tab
cdb = new CustomerDB(getApplicationContext());
int custcount = cdb.getCustomerCount();
if (custcount > 0) 
    List<Customers> cdbL = cdb.getAllCustomers();
    int listSize = cdbL.size();

    for (int i = 0; i < listSize; i++) 

        CustomerArray.add(new Customers(cdbL.get(i).getDBName()
                .toString(), cdbL.get(i).getDBAddress().toString(),
                cdbL.get(i).getDBTelNo().toString(), cdbL.get(i)
                        .getDBMobileNo().toString(), cdbL.get(i)
                        .getDBFaxNo().toString(), cdbL.get(i).getDBID()
                        .toString()));
    

现在,从您的列表中。将您的数据传输到您的自定义适配器

// set item into adapter
CustomerAdapter = new UserCustomAdapter(MainMaintenance.this,
        R.layout.maintenancetab, CustomerArray);
CustomerList = (ListView) findViewById(R.id.lvCustomer);
CustomerList.setItemsCanFocus(false);
CustomerList.setAdapter(CustomerAdapter);

在这里,您可以单击该项目并执行您想要的操作。在此示例中,我将我的数据传递到公共课程的列表中,然后我进入下一个活动。

    // get on item click listener
CustomerList.setOnItemClickListener(new OnItemClickListener() 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) 
        // TODO Auto-generated method stub
        Customers user = CustomerArray.get(arg2);
        user.setTID(user.getID());
        user.setTName(user.getName());
        user.setTAddress(user.getAddress());
        user.setTTelNo(user.getTelNo());
        user.setTMobileNo(user.getMobileNo());
        user.setTFaxNo(user.getFaxNo());

        Intent intent = new Intent(MainMaintenance.this,
                CustomerDetails.class);
        startActivity(intent);
    
);

您的自定义适配器中的 setOnClickListener 是您是否要在自定义适配器中放置一个按钮 :) 如果您感到困惑,请告诉我 :)

希望这个示例代码能给你一个思路,对你有所帮助。

【讨论】:

以上是关于Android:自定义列表视图,适配器中的表格视图的主要内容,如果未能解决你的问题,请参考以下文章

无法从android中的自定义列表视图中获取所选项目

在Android中按下提交按钮时使用自定义适配器从列表视图中获取选定项目

Android:自定义列表视图列表元素点击

ArrayIndexOutOfBoundsException 与 ListView 中的多个视图的自定义 Android 适配器

Android 自定义适配器无法正常用于聊天列表视图

Android - 通过复选框使用自定义适配器从列表视图中获取项目