当适配器具有零元素时,如何将布局用作列视图的空视图

时间:2014-03-12 05:14:25

标签: android listview android-listview

list.xml

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

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

            <TextView
                android:id="@android:id/empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab on icon to start a Campaign" />
        </LinearLayout>

活动+ list_demonstration.xml

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Active Campaign"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#4AE56B" />



    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Closed Campaign"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="130dp" >

        <ListView
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

我试图在application.in的启动时创建一个空的列表视图。其中textview将向icon上的选项卡提供信息,以及当用户选项卡和创建app时,listview中显示的数据。 .bt listview给出了一个错误,如何解决它..

Demo.java //自定义适配器

package com.example.smscampaign;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Demo extends ArrayAdapter{
    private Campaign_Details list;
        // used to keep selected position in ListView
       private int selectedPos = -1;   // init value for not-selected
        private Context context;
        private String[] values;

        public Demo(Context context, String[] values) {
            super(context, R.layout.activity_list_demostration);
            this.context = context;
            this.values = values;
        }



        public void setSelectedPosition(int pos){
        selectedPos = pos;
             // inform the view of this change
             notifyDataSetChanged();
        }
        public int getSelectedPosition(){
             return selectedPos;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                 View v = convertView;
                        LayoutInflater vi =   (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(R.layout.list, null);

                 // get text view
                     TextView label = (TextView)v.findViewById(R.id.text1);
                    ImageView btn=(ImageView)v.findViewById(R.id.imageView1);
                    if (convertView == null) {            
                        v = vi.inflate(R.layout.list, parent, false);
                    }
                    else
                        v = convertView;
                    TextView text1 = (TextView) v.findViewById(R.id.text1);

                    return v;
                }
        }

Campaign_Details.java

 package com.example.smscampaign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;



public class Campaign_Details extends Activity  {

    private Demo selectedAdapter;
    private ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        DataBaseHandler info= new DataBaseHandler(this);
        info.open();
        String data=info.getData();
        info.close();
        String[] values= new String[]{ data };      
        //txt.setText(data);
       // Button next=(Button) findViewById(R.id.next);
      //  Map<String, String[]> storage = new HashMap<String, String[]>();
     //   String[] tableItems = storage.get("ContactTable");


       // next.setOnClickListener(this);
    // final ListView listview = (ListView) findViewById(R.id.listview);

         ListView listview = (ListView) findViewById(R.id.listView1);
         TextView emptyText = (TextView)findViewById(android.R.id.empty);
         listview .setEmptyView(emptyText);
        // listview.setEmptyView((LinearLayout) findViewById(R.id.emptyText)); 
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < data.length(); ++i) {
            selectedAdapter = new Demo(this,values);
            selectedAdapter.setNotifyOnChange(true);


            listview.setAdapter(selectedAdapter);
          list.add(data);
        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, final View view,
              int position, long id) {
              Intent n = new Intent(getApplicationContext(), SmsSend.class);
            startActivity(n);

          }
        });
      }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.nextPage:
            Intent i = new Intent(this,SmsSend.class);
            startActivity(i);
            break;

        }
        return super.onOptionsItemSelected(item);
    }
    } 

1 个答案:

答案 0 :(得分:0)

更新:

你做错了什么。你需要改变如下。

   public Demo(Context context, String[] values) {
        super(context, R.layout.activity_list_demostration);
        this.context = context;
        this.values = values;
    }

   public Demo(Context context, String[] values) {
        super(context, values);
        this.context = context;
        this.values = values;
    }

Campaign_Details 活动

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_demostration);

    ListView listview = (ListView) findViewById(R.id.listView1);
    TextView emptyText = (TextView)findViewById(android.R.id.empty);

根据你的问题,你最后需要从

更改
 ListView listview = (ListView) findViewById(R.id.listView1);

 ListView listview = (ListView) findViewById(R.id.listView);