将数据从两个字符串数组提取到两个不同的布局到一个列表视图中

时间:2018-05-06 07:58:07

标签: android android-layout listview layout android-arrayadapter

我见过很多答案,我相应地更改了我的代码。现在我遇到了自己的问题。在运行这个适配器之后,我从list1获取了一个值,而list2只有两个值,两个值都有三个值。布局很好。我不知道是什么导致它而且我对arrayadapter也不太熟悉。这将是一个很大的帮助。提前谢谢。

public class AxisAdapter extends ArrayAdapter<String> {

    private ArrayList<String> list1 = new ArrayList<String>();
    private ArrayList<String> list2 = new ArrayList<String>();

    private Context context;
    private final int view1 = 0;
    private final int view2 = 1;

    public MyAdapter(Context context, ArrayList<String> records,ArrayList<String> records1) {
        super(context, 0, records);
        this.context = context;
        list1=records;
        list2=records1;

    }

    public int getViewTypeCount() {
        return 2;
    }

    public int getItemViewType(int position) {
        return (position == 0) ? view1 : view2;
    }

    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case view1: {
                final String item = list1.get(position);

                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.abc_layout, parent, false);
                }
                final TextView list_Txt = (TextView) convertView.findViewById(R.id.txtDef);
                Button list_But = (Button) convertView.findViewById(R.id.btnCall);

                list_Txt.setText(item.replaceAll("[^A-Za-z]", " "));


                list_But.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String number = item.replaceAll("[A-Za-z]", "");

                        Intent i = new Intent(Intent.ACTION_VIEW,
                                Uri.parse("tel:" + number));
                        context.startActivity(i);
                    }

                });
            }
            break;
            case view2: {
                final String item = list2.get(position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.url_layout, parent, false);
                }
                final TextView list_Txt = (TextView) convertView.findViewById(R.id.txtDetail);
                Button list_But = (Button) convertView.findViewById(R.id.btnVisit);
                list_Txt.setText(item.replaceAll("[^A-Za-z]", " "));
                list_But.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String number = item.replaceAll("[A-Za-z]", "");
                        Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("tel:12345"));
                        context.startActivity(i);
                    }
                });
            }
            break;


        }


        return convertView;
    }
}

在我的活动课

public class AnActivity extends AppCompatActivity {

    Toolbar toolbar;
    AxisAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_axis);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if(getSupportActionBar()!= null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        ListView list = (ListView) findViewById(R.id.axisList);
        String[] ussd = getResources().getStringArray(R.array.axisCode);
        String[] links = getResources().getStringArray(R.array.axisLink);

        ArrayList<String> myData = new ArrayList<String>();
        ArrayList<String> myLinks = new ArrayList<String>();


        for(int i = 0; i<ussd.length;i++) {
            myData.add(ussd[i]);
        }

        for(int i =0;i<links.length;i++){
            myLinks.add(links[i]);

        }

        adapter = new AxisAdapter(this,myData,myLinks);
        list.setAdapter(adapter);

        //adapter= new AxisAdapter(this,myLinks);
        //list.setAdapter(adapter);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home)
            finish();


        return super.onOptionsItemSelected(item);
    }
}

在我的两个布局类中,有一个按钮和一个文本框。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="5dp"
        android:background="@color/textboxColor">

        <TextView
            android:id="@+id/txtDef"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:fontFamily="@font/roboto_slab"
            android:padding="5dp"
            android:text="TextView"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btnCall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="0"
            android:fontFamily="@font/roboto_slab_bold"
            android:text="Call"
            android:theme="@style/PrimaryButton" />

    </LinearLayout>
</LinearLayout>
另一个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="5dp"
        android:background="@color/textboxColor">

        <TextView
            android:id="@+id/txtDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:fontFamily="@font/roboto_slab"
            android:padding="5dp"
            android:text="TextView"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btnVisit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="0"
            android:fontFamily="@font/roboto_slab_bold"
            android:text="Visit"
            android:theme="@style/PrimaryButton" />

    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您的关注:

  

我从list1获取一个值,而list2中只有两个值   两者都有三个值

你面临的问题在于:

private final int view1 = 0;
private final int view2 = 1;

...

public int getItemViewType(int position) {
    return (position == 0) ? view1 : view2;
}

然后在getView()方法中调用getItemViewType方法:

int viewType = getItemViewType(position);

位置的值范围从0到列表中的项目数减去1.因此,如果position = 0,则getItemViewType()方法始终返回0(view1),如果position = 1,2则返回1(view2) ,3 ......

然后写下:

    switch (viewType) {
                case view1: {
                    final String item = list1.get(position);

                 ...

                            }
                break;
            case view2: {
                final String item = list2.get(position);

                 ...

                        }
                break;
                    }

case view1中,它只是指示列表中第一个位置为0的项目。只有一个项目停留在位置0,因此final String item = list1.get(position);只被调用一次,这就是为什么它返回1个值。

case view2中,它只是指示位置1,2,3处的项目......如您所说,列表中有3个项目,适配器将为列表视图生成3个项目,{ {1}}将等于位置1和2.因此case view2被调用两次,这就是它返回2个值的原因。

我不知道你的下一步会是什么,因为我不明白你的期望。

更新: 具体解决方案:

final String item = list2.get(position);

答案 1 :(得分:0)

尝试发送一些字符串,如下面的情况一个字符串,以确定类型,你实际上想要切换的内容,在我的情况下,它是每月,每年等等,根据该字符串,您可以使用不同的视图或也许可以轻松地显示替代布局。

public class incomeadapter extends ArrayAdapter {
private List<Incomemodel> getdatas;
private int resource;
String incometype;
private LayoutInflater inflater;
Context context;

public incomeadapter(Context context, int resource, List<Incomemodel> getdata, String type) {
    super(context, resource, getdata);
    this.context = context;
    getdatas = getdata;
    incometype = type;
    this.resource = resource;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    TextView t1, t2;

    if (convertView == null) {

        if (incometype.equals("monthly")) {
            convertView = inflater.inflate(R.layout.incomemonthly_item, null);


            t1 = (TextView) convertView.findViewById(R.id.text2);
            t2 = (TextView) convertView.findViewById(R.id.text1);
            t1.setText(getdatas.get(position).getMonths());
            t2.setText(getdatas.get(position).getIncome());

        }
        if (incometype.equals("yearly")) {
            convertView = inflater.inflate(R.layout.incomeyearly_item, null);


            t1 = (TextView) convertView.findViewById(R.id.text2);
            t2 = (TextView) convertView.findViewById(R.id.text1);
            t1.setText(getdatas.get(position).getYear());
            t2.setText(getdatas.get(position).getIncome());

        }

    }


    return convertView;
}
}

<强> PS。尝试将recyclelerview与多个视图一起使用,这样我就可以轻松实现