2016-11-12 88 views
0

我正在创建此布局,其中除了每个项目之外,还会显示一个文本列表,并显示一个“删除”按钮。我猜不到我的布局正确。我可以得到一些帮助吗?Android编程 - 带按钮的ListView

这里是列表视图中的布局文件:

<?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" 
android:orientation="vertical" > 



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

     <Button 
      android:id="@+id/delete" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFFFF" 
      android:text="Delete" 
      android:onClick="onClickDelete"/> 

    </ListView> 

</RelativeLayout> 

下面是Java代码:

package com.android.ict.seneca.androidpocketguide; 


import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.res.TypedArray; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 

public class Websites extends Activity 
     implements AdapterView.OnItemClickListener{ 

    private final String CLASS_NAME = "Websites"; 

    DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm"); 
    String date; 
    String location; 
    String name; 
    int counter; 
    List listName; 

    private SharedPreferences savedState; 

    private ListView listView; 
    private List<RowItem> rowItems; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_websites); 
     listName= new ArrayList(); 
     String app_name = "//sdcard//LocoLog.txt"; 
     //int counter = 0, flag = 0; 
     String temp = ""; 
     savedState = getSharedPreferences("quantitySaved", MODE_PRIVATE); 
     rowItems = new ArrayList<RowItem>(); 
     counter = savedState.getInt("counter", -1); 
     if(counter!=-1) 
     { 
      for (int i = 1; i <= counter; i++) 
      { 
       String na; 
       String da; 
       String lo; 
       na = "name" + i; 
       da = "date" + i; 
       lo = "location" + i; 

       name = savedState.getString(na, "na"); 
       date = savedState.getString(da, "da"); 
       location = savedState.getString(lo, "lo"); 

       //Toast.makeText(this, "The data: "+name + " " + location + " " + date,Toast.LENGTH_LONG).show(); 
       listName.add(new RowItem(name, location, date, counter)); 
      } 
     } 
     else 
     { 
      Toast.makeText(this, "No locations saved yet",Toast.LENGTH_LONG).show(); 
     } 

     listView = (ListView) findViewById(R.id.locationList); 
     listView.setAdapter(new CustomListAdapter(this, R.layout.list_item, listName)); 
     listView.setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     String cities = String.valueOf(parent.getItemAtPosition(position)); 
     Toast.makeText(this, cities, Toast.LENGTH_LONG).show(); 
     Intent myIntent = new Intent(view.getContext(), SingleListItem.class); 
     myIntent.putExtra("product", cities); 
     startActivityForResult(myIntent, 0); 
    } 
    public void onClickDelete(View view) 
    { 
     //String cities = String.valueOf(parent.getItemAtPosition(position)); 
     Toast.makeText(this, "delete ", Toast.LENGTH_LONG).show(); 
     //rowItems.remove(position); 
    } 

    public void onClickAdd(View view) 
    { 
     Toast.makeText(this, "add ", Toast.LENGTH_LONG).show(); 

    } 

    public void onStart() { 
     super.onStart(); 
     Log.d(CLASS_NAME, "onStart invoked!"); 
    } 

    public void onPause() { 
     super.onPause(); 
     Log.d(CLASS_NAME, "onPause invoked!!"); 
    } 

    public void onResume() { 
     super.onResume(); 
     Log.d(CLASS_NAME, "onResume invoked!!"); 
    } 

    public void onStop() { 
     super.onStop(); 
     Log.d(CLASS_NAME, "onStop invoked!!!"); 
    } 

    public void onDestroy() { 
     super.onDestroy(); 
     Log.d(CLASS_NAME, "onDestroy INVOKED!!!"); 
    } 

    public void onRestart() { 
     super.onRestart(); 
     Log.d(CLASS_NAME, "onRestart invoked!!"); 
    } 

} 

回答

1

您不能添加按钮像这样的listview.Your按钮应该是分离的布局。

mainLayout.xml

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

和您的列表视图的项目布局,即按钮在你的情况 buttons.xml

<Button 
    android:id="@+id/delete" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FFFFFFFF" 
    android:text="Delete" 
    android:onClick="onClickDelete"/> 

,然后在活动

listview.setAdapter(YourAdapter()); 

,然后在你的适配器的getView方法中()

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolderItem viewHolder; 
    if(convertView==null){ 
     LayoutInflater inflater = ((Activity) mContext).getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.buttons, parent, false); 
     viewHolder = new ViewHolderItem(); 
     viewHolder.button = convertView.findViewById(R.id.button); 
     convertView.setTag(viewHolder); 
    }else{ 
     viewHolder = (ViewHolderItem) convertView.getTag(); 
    } 

    viewHolder.button.setOnClickListener(new View.onClickListener(){ 
     public void onClick(){ 
     //handle your on click. 
     } 
    }); 

    return convertView; 
} 

Viewholder类

static class ViewHolderItem{ 
    private Button button; 
} 

阅读此了解更多信息http://www.androidhive.info/2011/10/android-listview-tutorial

+0

非常感谢您的帮助。我现在得到另一个错误,我猜是因为我没有初始化按钮的地方?:java.lang.NullPointerException:尝试调用虚拟方法'无效android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'上空对象引用 –