ListView白色屏幕

时间:2013-07-22 22:57:38

标签: android listview android-activity

我的ListView有问题,我不知道如何解决它。我启动活动时只有一个白色屏幕。你能帮我解决一下吗?

以下是两个xml文件:

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

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

 </RelativeLayout>

以及列表布局:

<?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="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""/>

<TextView
    android:id="@+id/category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/name"
    android:layout_marginTop="5dip"
    android:text=""/>

<TextView
    android:id="@+id/distance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/category"
    android:layout_alignBottom="@+id/category"
    android:layout_alignParentRight="true"
    android:text=""/>

</RelativeLayout>

活动:

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class ProxiRestaurantsListActivity extends SherlockActivity {

// Declare Variables
ListView list;
RestaurantsListAdapter adapter;
String[] name;
String[] category;
String[] distance;


public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.listview, container, false);

    // Generate sample data
    name = new String[] { "Restaurant1", "Restaurant2", "Restaurant3" };

    category = new String[] { "Greek Tavern", "Italian", "Fish" };

    distance = new String[] { "0.5Km", "1.2Km", "1.9Km" };

    // Locate the ListView in listview_main.xml
    list = (ListView) view.findViewById(R.id.listview);

    // Pass results to ListViewAdapter Class
    adapter = new RestaurantsListAdapter(getBaseContext(), name, category, distance);
    // Binds the Adapter to the ListView
    list.setAdapter(adapter);
    // Capture button clicks on ListView items
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // Send single item click data to SingleItemView Class
            Intent i = new Intent(getBaseContext(), SingleRestaurantViewActivity.class);
            // Pass all data rank
            i.putExtra("name", name);
            // Pass all data country
            i.putExtra("category", category);
            // Pass all data population
            i.putExtra("distance", distance);
            // Pass a single position
            i.putExtra("position", position);
            // Open SingleItemView.java Activity
            startActivity(i);
        }

    });
    return view;
}

   public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getSupportMenuInflater();
       inflater.inflate(R.menu.main, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle item selection
       switch (item.getItemId()) {        
       case R.id.Settings:
        Intent i = new Intent(getBaseContext(), SettingsActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i); 
           return true;
       default:
           return super.onOptionsItemSelected(item);
       }
   }    
 }

和listview适配器:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MuseumListAdapter extends BaseAdapter {

// Declare Variables
Context context;
String[] name;
String[] category;
String[] distance;
LayoutInflater inflater;

public MuseumListAdapter(Context context, String[] name, String[] category,
        String[] distance ) {
    this.context = context;       
    this.name = name;
    this.category = category;
    this.distance = distance;       
}

@Override
public int getCount() {
    return name.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    // Declare Variables
    TextView txtname;
    TextView txtcategory;
    TextView txtdistance;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.museumlistlayout, parent, false);

    // Locate the TextViews in listview_item.xml
    txtname = (TextView) itemView.findViewById(R.id.name);
    txtcategory = (TextView) itemView.findViewById(R.id.category);
    txtdistance = (TextView) itemView.findViewById(R.id.distance);        

    // Capture position and set to the TextViews
    txtname.setText(name[position]);
    txtcategory.setText(category[position]);
    txtdistance.setText(distance[position]);

    return itemView;
}
}

我希望你掌握所有相关信息。

非常感谢。

0 个答案:

没有答案