当我单击列表项时,我想将其名称存储在edittext中

时间:2016-04-22 18:14:51

标签: android jquery list listview

我正在创建一个像许多现有应用程序一样的电子钱包应用程序。我有很多不同的课程,但我需要一些建议。我创建了一个listview女巫获取我创建并保存在phpMyAdmin数据库中的类别。我正在列表视图中成功显示此数据。此列表视图仅显示用户的类别列表,如果他单击列表项,则会激活他以编辑或删除列表项的新活动。 现在我使用相同的代码(我使用相同的代码!)在对话框中创建第二列表视图。想象一下,当我单击具有单击侦听器的编辑文本时,此列表视图将出现在对话框内。我也成功地在第二个列表视图中显示数据。现在在此对话框中,当我单击列表项/类别(比如说体育类别)时,我希望编辑文本的名称更改为我选择的类别。但我很困惑,因为我使用相同的代码和方法在我的第一个列表视图上打开编辑和删除活动。我希望我没有太多困惑你。

以下是在列表视图中下载数据的代码。

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;

public class ExParserActivity extends AsyncTask<Void, Integer, Integer>
{
Context context;
ListView exListView;
String data;

ArrayList<String> exCategories = new ArrayList<>();
ProgressDialog progressDialog;

public ExParserActivity(Context context, String data, ListView lv)
{
    this.context = context;
    this.data = data;
    this.exListView = lv;
}

//B4 JOB STARTS
@Override
protected void onPreExecute()
{
    super.onPreExecute();

    progressDialog = new ProgressDialog(context);
    progressDialog.setTitle("Parser");
    progressDialog.setMessage("Parsing...Please wait");
    progressDialog.show();
}

//Heavy job
@Override
protected Integer doInBackground(Void... params)
{
    return this.parse();
}

@Override
protected void onProgressUpdate(Integer... values)
{
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Integer integer)
{
    super.onPostExecute(integer);

    progressDialog.dismiss();

    if(integer == 1)
    {
        //ADAPTER
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, exCategories);

        //ADAPT TO LIST VIEW
        exListView.setAdapter(arrayAdapter);

        //ON CLICK LISTENER
        exListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                context.startActivity(new Intent(context, UpdateDeleteExCategories.class));
                //Snackbar.make(view, exCategories.get(position), Snackbar.LENGTH_SHORT).show();
            }
        });
    }

    else
    {
        Toast.makeText(context, "Unable to Parse", Toast.LENGTH_LONG).show();
    }
}

//PARSE RECEIVED DATA
private int parse()
{
    try
    {
        //ADD THAT DATA TO JSON ARRAY FIRST
        JSONArray jsonArray = new JSONArray(data);

        //CREATE JO OBJECT TO HOLD A SINGLE ITEM
        JSONObject jsonObject = null;

        exCategories.clear();

        //LOOP THROUGH THE ARRAY
        for(int i = 0; i < jsonArray.length(); i++)
        {
            jsonObject = jsonArray.getJSONObject(i);

            //RETRIEVE NAME
            String exCatName = jsonObject.getString("exCatName");

            //ADD IT TO ARRAY LIST
            exCategories.add(exCatName);
        }

        //IF ITS SUCCESSFUL RETURN 1
        return 1;
    }

    catch (JSONException e)
    {
        e.printStackTrace();
    }

    //IF IT IS NOT SUCCESSFUL RETURN 0
    return 0;
}
}

第二课

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Calendar;

public class AddExpenseActivity extends SwipeFunctionActivity
{
private ImageView backImageView;
private TextView setDateTextView;
private EditText categoryEditText, amountEditText;
int year, month, day;
static final int DIALOG_ID = 0; //Initialise the variable to 0.

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

    final Calendar calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);

    setDateTextView = (TextView)findViewById(R.id.setDateTextView);
    backImageView = (ImageView)findViewById(R.id.backImageView);
    categoryEditText = (EditText)findViewById(R.id.categoryEditText);
    amountEditText = (EditText)findViewById(R.id.amountEditText);

        setDateTextView.setOnClickListener(new View.OnClickListener()
        {
        @Override
        public void onClick(View v)
        {
            showDialog(DIALOG_ID);
        }
    });

    categoryEditText.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            pickCategoryMethod();
        }
    });

    backImageView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            backMethod();
        }
    });
   }

@Override
protected Dialog onCreateDialog(int id)
{
    if (id == DIALOG_ID)
    {
        return new DatePickerDialog(this, dpickerListener, year, month, day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener dpickerListener = new DatePickerDialog.OnDateSetListener()
{
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {
        AddExpenseActivity.this.year = year;
        month = monthOfYear + 1;
        day = dayOfMonth;

        showDate(AddExpenseActivity.this.year, month, day);
        //Toast.makeText(AddExpenseActivity.this, "Selected date: " + dayOfMonth + " / " + (monthOfYear+1) + " / " + year, Toast.LENGTH_LONG).show();
    }
};

private void showDate(int year, int month, int day)
{
    setDateTextView.setText(new StringBuilder().append(day).append("/")
            .append(month).append("/").append(year));
}

public void pickCategoryMethod()
{
    String url = "http://192.168.0.3/myapp/fexcateg.php";
    //

    final Dialog dialog = new Dialog(AddExpenseActivity.this);
    dialog.setContentView(R.layout.activity_pick_category);
    dialog.setTitle("         Pick your Category");

    final ListView pickCategoryListView = (ListView)dialog.findViewById(R.id.pickCategoryListView);
    final ExDownloaderActivity downloader = new ExDownloaderActivity(this, url, pickCategoryListView);

    //Execute download
    downloader.execute();

    //pickCategoryListView = (ListView)dialog.findViewById(R.id.pickCategoryListView);

    //This makes the dialog visible.
    dialog.show();
}

public void backMethod()
{
    //startActivity(new Intent(getApplicationContext(), BalanceActivity.class));
    finish();
}

@Override
public void onSwipeLeft()
{
    super.onSwipeLeft();
    startActivity(new Intent(getApplicationContext(), AddIncomeActivity.class));
    finish();
}

@Override
public void onSwipeRight()
{
    super.onSwipeRight();
    startActivity(new Intent(getApplicationContext(), AddSavingActivity.class));
    finish();
}
}

第三类

{{1}}

所以现在我的问题。我想在单击对话框内的类别时,我在trird类中具有categoryEditText变量,以更改我在listview中单击的项目的名称。首先,我必须在哪里编写此代码,其次是如何编写代码。我有点失落因为真的是一个很大的应用程序:/ 非常感谢!!!

0 个答案:

没有答案