简体   繁体   English

每行具有TextView和EditText的Listview

[英]Listview with TextView and EditText per line

I have to come to you for assistance with a particular activity in my android application. 我必须向您寻求有关我的android应用程序中特定活动的帮助。 I would like to have a list of item descriptions with a textfield next to it. 我想要一个项目描述列表,旁边有一个textfield This textfield is used to put in a quantity. textfieldtextfield用于输入数量。

I am thinking about doing this by using a listview with a custom row layout but am wondering wether or not this is the right way to go for this. 我正在考虑通过使用具有自定义行布局的listview来做到这一点,但我想知道是否这是正确的方法。

I will fill the list from code since these items are fetched from a webservice. 我将从代码中填充列表,因为这些项是从Web服务获取的。

So what is the best way to accomplish this? 那么实现此目标的最佳方法是什么? Use a listview or use a different view? 使用列表视图还是使用其他视图? Can I write to my domain object after the edittext editing was finished? 编辑文本编辑完成后,我可以写入域对象吗?

The way you are talking about is to create ListView with focusable EditText inside it. 您正在谈论的方法是创建带有可聚焦EditText的ListView。

For the same you need to create a custom adapter for listview, create a raw layout with one EditText and inflate the same for every row by creating an adapter class. 为此,您需要为列表视图创建一个自定义适配器,使用一个EditText创建一个原始布局,并通过创建一个适配器类为每一行添加相同的布局。

Detailed example is here: Android Focusable EditText inside ListView 详细示例在这里: ListView内的Android Focusable EditText

This is the example of what I've done for my current project. 这是我为当前项目所做的示例。

//java for my class 
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class editpage extends ListActivity {
    int editCount;
    private dbadapter mydbhelper;

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

        setContentView(R.layout.edit_list);
        mydbhelper = new dbadapter(this);
        mydbhelper.open();
        fillData();
    }



    private void fillData() {


        Cursor e = mydbhelper.getUserWord();
            startManagingCursor(e);

     // Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
        String[] from = new String[] {dbadapter.KEY_USERWORD,};

        // an array of the views that we want to bind those fields to (in this case text1,text2,text3)
        int[] to = new int[] {R.id.textType,};

        // Now create a simple cursor adapter and set it to display
       SimpleCursorAdapter editadapter = 
       new SimpleCursorAdapter(this, R.layout.edit_row, e, from, to);
       ListView list = getListView();
       View footer = getLayoutInflater().inflate(R.layout.footer_layout, list, false);
       list.addFooterView(footer);
       setListAdapter(editadapter);
       editCount = e.getCount();
        }

        public void onClick(View footer){
            final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
                editClickSound.start();
                    };


@Override
protected void onListItemClick(ListView list, View v, int position, long id)
{
    super.onListItemClick(list, v, position, id);
    //final Intent intent = new Intent(this, title.class);
    //startActivityForResult(intent, position);
    }

Then for my cursor, note that I have multi where statements. 然后,对于我的光标,请注意我有多个where语句。 I do this bc I have a multi lvl listview. 我这样做是因为我有一个多级列表视图。 categories ->source -> title -> user word. 类别->来源->标题->用户词。

    // retrieves all the descriptions for the edittext fields
      public  Cursor getUserWord() 
        {
            return myDataBase.query(USER_WORD_TABLE, new String[] {
                    KEY_ID, 
                    KEY_CATEGORY,
                    KEY_SOURCE, KEY_TITLE, KEY_EDITWORD, KEY_USERWORD 
                    }, 
                    KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "=" 
                    +source.sourceClick + " AND " + KEY_TITLE+ "=" + title.titleClick, 
                    null, null, null, KEY_ID);

        }

Lastly I have my xml to display this. 最后,我有我的xml显示。

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

    <ListView android:id="@+id/android:list"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"/>

</LinearLayout>

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

    <TextView
        android:id="@+id/textType"
        android:layout_width="150dip"
        android:layout_height="fill_parent"
        android:textSize="10dip"
        android:gravity="center|center_vertical"
        android:height="30dip"
        android:text="spaceholder" 
        android:layout_gravity="center_vertical" 
        android:layout_margin="2dp"/>

    <EditText
        android:id="@+id/editText"
        android:layout_width="160dip"
        android:layout_height="30dip"
        android:layout_gravity="left"
        android:gravity="left"
        android:textSize="10dip"
        android:height="25dip">
        </EditText>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <Button
        android:id="@+id/confirminedit"
        android:layout_weight="0.2"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:text="@string/confirmbutton"
        android:onClick="onClick"
        android:layout_alignParentBottom="true" />

</LinearLayout>

Last XML is for a footer with a confirm button. 最后一个XML用于带有确认按钮的页脚。 So after the user fills the edittext it will take the data and store it for the next activity to use. 因此,在用户填充edittext之后,它将获取数据并将其存储以供下一个活动使用。 This function is what I'm currently working on but this code should give you a good idea on what you asked about. 该功能是我目前正在使用的功能,但是这段代码应该可以使您对所要询问的内容有个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM