ListView由可序列化对象的ArrayList组成

时间:2016-02-22 21:40:14

标签: android listview serialization

现在我有一个名为Tasks的可序列化对象类,我打算创建一个Listview,每个对象代表一个列表中的项目但是当我运行代码时它不起作用

这是主要活动:

public class MainActivity extends ActionBarActivity {
    EditText NameET, ImportanceED, dateED, TimeED;
    String Name, Importance, date, Time;

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            NameET = (EditText) findViewById(R.id.NameeditText);
            ImportanceED = (EditText) findViewById(R.id.ImportanceeditText2);
            dateED = (EditText) findViewById(R.id.dateeditText3);
            TimeED = (EditText) findViewById(R.id.timeeditText4);

        public void GetTexts() {


        Name = NameET.getText().toString();
        Importance = ImportanceED.getText().toString();
        date = dateED.getText().toString();
        Time = TimeED.getText().toString();
    }

    public void AddTask(View view) {
        String testName = NameET.getText().toString().trim();
        String testImportance = ImportanceED.getText().toString().trim();
        String testDate = dateED.getText().toString().trim();
        String testTime = TimeED.getText().toString().trim();


            GetTexts();

 OpenDetailsActivity();

        }
    }

    public void OpenDetailsActivity() {
        Intent DetailsIntent = new Intent(this, DetailsActivity.class);
        DetailsIntent.putExtra("Name",Name );
        DetailsIntent.putExtra("Importance",Importance );
        DetailsIntent.putExtra("date",date );
        DetailsIntent.putExtra("Time",Time );

        startActivity(DetailsIntent);


    }

这是包含活动的列表:

公共类DetailsActivity扩展了Activity {

ListView list;
String Name;
String Importance;
String date;
String Time ;

ArrayList <Tasks>TaskList = new ArrayList();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.task_list);
    Intent intent = getIntent();
    Name= intent.getStringExtra("Name");
    Importance= intent.getStringExtra("Importance");
    date= intent.getStringExtra("date");
    Time= intent.getStringExtra("Time");
    addToList();


}

public void addToList() {
    int i ;
    int N = TaskList.size();
    if (N > 0 ){
        i = N + 1 ;

    } else {
        i= 0 ;
    }

一切正常,直到这里,列表显示为空白

    TaskList.add(i, new Tasks(Name, Importance, date, Time));



    ListAdapter adapter = new ListAdapter(this, TaskList);
    list = (ListView) findViewById(R.id.listView_tasks);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view,
                                                            int position, long id) {

                                        String SelectedTask = TaskList.get(position).getName();
                                        Toast.makeText(getApplicationContext(), SelectedTask, Toast.LENGTH_SHORT).show();

这是任务可序列化类

class Tasks implements Serializable  {

    String name="";
     String date="";
    String time = "";
    String importance="";

    public Tasks(String name, String importance, String date , String time) {
        this.date = date;
        this.importance = importance;
        this.name = name;
        this.time = time;
    }


    public String getDate() {
        return date;
    }


    public String getImportance() {
        return importance;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTime() {
        return time;
    }

这是自定义适配器

public class ListAdapter extends ArrayAdapter {

        private final Activity context;
    List<Tasks> TaskList;

    public ListAdapter(Activity context, List<Tasks> TaskList) {
            super(context, R.layout.row_layout);

            this.context=context;
            this.TaskList=TaskList;
        }

        public View getView(int position,View view,ViewGroup parent) {
            LayoutInflater inflater=context.getLayoutInflater();
            View rowView=inflater.inflate(R.layout.row_layout, null , false);

            TextView Name = (TextView) rowView.findViewById(R.id.NameTV);
            TextView Date = (TextView) rowView.findViewById(R.id.dateTV);
            TextView Importance = (TextView) rowView.findViewById(R.id.importanceTV);
            TextView Time = (TextView) rowView.findViewById(R.id.timeTV);

            TaskList.get(position).getName();

            Name.setText(TaskList.get(position).getName());
            Date.setText(TaskList.get(position).getDate());
            Importance.setText(TaskList.get(position).getImportance());
            Time.setText(TaskList.get(position).getTime());

            return rowView;

这是task_list布局

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

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

和这个row_layout

   <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/NameTV"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/importanceTV" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/dateTV" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/timeTV" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

我在你的代码中做了一些更改,我在我的电脑上进行了测试,它正在运行。

我只更新ListAdapter.java并且它可以工作。

ListAdapter.java

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Usman Asghar on 23/02/2016.
 */
public class ListAdapter extends ArrayAdapter {

   Context context;
   List<Tasks> TaskList;


    public ListAdapter(Activity context, List<Tasks> TaskList) {
        super(context, R.layout.row_layout,TaskList);
        this.context = context;
        this.TaskList = TaskList;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row_layout, null, false);
        }
        TextView Name = (TextView) view.findViewById(R.id.NameTV);
        TextView Date = (TextView) view.findViewById(R.id.dateTV);
        TextView Importance = (TextView) view.findViewById(R.id.importanceTV);
        TextView Time = (TextView) view.findViewById(R.id.timeTV);

        Name.setText(TaskList.get(position).getName());
        Date.setText(TaskList.get(position).getDate());
        Importance.setText(TaskList.get(position).getImportance());
        Time.setText(TaskList.get(position).getTime());
        return view;
    }
}

我将ArrayAdapter构造函数更改为

 super(context, R.layout.row_layout);

 super(context, R.layout.row_layout,TaskList);

在我们没有指定要在ListView上显示的数据之前,ArrayAdapter不起作用。

我在getView()方法中做了一些小改动。

答案 1 :(得分:0)

向您的适配器添加此方法:

@Override
public int getCount() {
    return TaskList.size();
}

改变这个:

TaskList.add(i, new Tasks(Name, Importance, date, Time));

到此:

TaskList.add(new Tasks(Name, Importance, date, Time));