自定义ListView不起作用

时间:2016-08-28 22:00:27

标签: java android listview android-arrayadapter

我刚写了这段代码 它的工作原理如下: 它获取一个数字和一个字符串+一个图像并将它们放入数组列表然后将它们放入ListView,但有些它是如何工作的,我无法找到问题

MainActivity

package com.arashsm79.asm.mathcity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;



public class MainActivity extends AppCompatActivity {

    public ListView list;
    public ArrayList<Integer> id = new ArrayList<>();;
    public ArrayList<String> names = new ArrayList<>();;
    public ArrayList<Bitmap> images = new ArrayList<>();;
    private static final int CAMERA_REQUEST = 1888;
    public static Bitmap ph;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // <View>
        list = (ListView) findViewById(R.id.listView);
        final TextView id_tv = (TextView) findViewById(R.id.id_tv);
        final TextView names_tv = (TextView) findViewById(R.id.name_id);

        Button done = (Button) findViewById(R.id.done_btn_id);
        Button cancel = (Button) findViewById(R.id.done_btn_id);
        Button img = (Button) findViewById(R.id.img);
        // </Views>



        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(id_tv.getText() != null && names_tv.getText() != null && ph != null) {
                    id.add(Integer.parseInt(String.valueOf(id_tv.getText())));
                    names.add(String.valueOf(names_tv.getText()));
                    images.add(ph);
                }else Toast.makeText(getApplicationContext(), "از پر کردن جاهای خالی و قرار دادن عکس اطمینان حاصل فرمایید", Toast.LENGTH_SHORT).show();
                id_tv.setText("");
                names_tv.setText("");
                ph = null;

            }
        });

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });


        ListConfig adapter = new ListConfig(MainActivity.this, id, names, images);
        list.setAdapter(adapter);





    }




    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ph = photo;
        }
    }









}

这是MainActivity的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.arashsm79.asm.mathcity.MainActivity"
    android:background="#cff9cd">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:text="Done"
        android:id="@+id/done_btn_id"
        android:background="@drawable/buttons"
        android:layout_below="@+id/listView"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="-5dp"
        android:textStyle="bold" />

    <Button
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Cancel"
        android:id="@+id/cancel_btn_id"
        android:layout_alignStart="@+id/done_btn_id"
        android:background="@drawable/cancel"
        android:layout_marginTop="440dp"
        android:textStyle="bold" />

    <EditText
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/id_tv"
        android:layout_below="@+id/listView"
        android:layout_toStartOf="@+id/done_btn_id"
        android:layout_marginTop="13dp"
        android:layout_marginRight="5dp"
        android:background="@android:drawable/checkbox_off_background"
        android:hint="ID"
        android:paddingLeft="15dp"
        android:textStyle="bold" />

    <EditText
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/name_id"
        android:layout_below="@+id/listView"
        android:layout_toStartOf="@+id/id_tv"
        android:layout_alignBottom="@+id/id_tv"
        android:textStyle="bold" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="40dp"
        android:layout_height="70dp"
        android:text="img"
        android:id="@+id/img"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/img"
        android:textStyle="bold" />
</RelativeLayout>

这是我的Adapter类

package com.arashsm79.asm.mathcity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;


public class ListConfig extends ArrayAdapter<Bitmap> {
    public ArrayList<Integer> id_config = new ArrayList<>();;
    public ArrayList<String> names_config = new ArrayList<>();;
    public ArrayList<Bitmap> images_config = new ArrayList<>();;
    public Activity context_config;


    public ListConfig(Activity context, ArrayList<Integer> id, ArrayList<String> names, ArrayList<Bitmap> images) {
        super(context,R.layout.list, images);
        context_config = context;
        id_config = id;
        names_config = names;
        images_config = images;
    }

    public View getView(int position, View view, ViewGroup parent){
        LayoutInflater inf = context_config.getLayoutInflater();
        View row = inf.inflate(R.layout.list, null, true);
        TextView name = (TextView) row.findViewById(R.id.tozihat);
        TextView id = (TextView) row.findViewById(R.id.shomare);
        ImageView image = (ImageView) row.findViewById(R.id.aks);

        name.setText(names_config.get(position));
        id.setText(id_config.get(position));
        image.setImageBitmap(images_config.get(position));;
        return row;
    }
}

最后一个是列表中每行的xml

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

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/aks"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="220dp"
        android:layout_height="100dp"
        android:id="@+id/tozihat"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/aks"
        android:textAlignment="viewEnd"
        android:padding="5dp"
        android:textStyle="bold"
        android:textSize="20dp"
        android:hint="توضیحات" />

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/shomare"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="27dp"
        android:paddingLeft="0dp"
        android:paddingTop="15dp"
        android:textAlignment="center"
        android:hint="شماره" />

</RelativeLayout>

这是我得到的错误

08-29 02:11:57.678 21741-21741/com.arashsm79.asm.mathcity E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.arashsm79.asm.mathcity, PID: 21741
                                                                            android.content.res.Resources$NotFoundException: String resource ID #0x5
                                                                                at android.content.res.Resources.getText(Resources.java:286)
                                                                                at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                                at android.widget.TextView.setText(TextView.java:4148)
                                                                                at com.arashsm79.asm.mathcity.ListConfig.getView(ListConfig.java:42)
                                                                                at android.widget.AbsListView.obtainView(AbsListView.java:2467)
                                                                                at android.widget.ListView.makeAndAddView(ListView.java:1864)
                                                                                at android.widget.ListView.fillDown(ListView.java:698)
                                                                                at android.widget.ListView.fillFromTop(ListView.java:759)
                                                                                at android.widget.ListView.layoutChildren(ListView.java:1673)
                                                                                at android.widget.AbsListView.onLayout(AbsListView.java:2271)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                                at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                                at android.view.View.layout(View.java:15695)
                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4981)
                                                                                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:596)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:557)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:155)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
08-29 02:16:57.798 21741-21741/com.arashsm79.asm.mathcity D/Process: killProcess, pid=21741
08-29 02:16:57.798 21741-21741/com.arashsm79.asm.mathcity D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

2 个答案:

答案 0 :(得分:0)

我在这行看到了你的代码:

id.add(Integer.parseInt(String.valueOf(id_tv.getText())));

这意味着您获取String并将其解析为Integer,因为我认为String值是有意义的,因此它不能是String资源ID。这就是原因。

因此,解决此问题的最简单方法是替换您的代码(在Adapter.getView()中):

id.setText(id_config.get(position));

id.setText(""+id_config.get(position));

答案 1 :(得分:0)

替换行:

id.setText(id_config.get(position));

with:

id.setText(String.valueOf(id_config.get(position)));