Click here to Skip to main content
15,883,127 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a large product list for a store. I want users to be able to click on items in the list so that it can be added into a smaller user list after it is clicked.

What I have tried:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import static com.example.android.loginregister.R.id.list;

/**
* Created by keaberry on 3/8/2017.
*/

public class CreateList2 extends AppCompatActivity implements
AdapterView.OnItemClickListener {
String[] items;
ArrayList<string> listItems;
ArrayAdapter<string> adapter;
ListView listView;
EditText editText;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_list2);
listView=(ListView)findViewById(R.id.listview);
editText=(EditText)findViewById(R.id.txtsearch);
initList();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {

}


@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")){
initList();
}
else{
searchItem(s.toString());
}
}

@Override
public void afterTextChanged(Editable s) {

}
});

}

public void searchItem(String textToSearch){
for (String item:items){
if(!item.contains(textToSearch)){
listItems.remove(item);
}
}

adapter.notifyDataSetChanged();

}


public void initList(){
items=new String[]{"Bacon", "Milk","Cereal","Frozen Pizza","Sports Drinks","Cheese","Detergent","Chips","Pet Food"};
listItems=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<string>(this, R.layout.item_list, R.id.txtitem, listItems );
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);


}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp=(TextView) view;
String Value = listView.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), Value, Toast.LENGTH_SHORT).show();
}
Posted
Updated 13-Apr-17 12:01pm
Comments
Richard Deeming 6-Apr-17 14:20pm    
You've told us what you want, and you've shown us the code you've written. But you haven't told us where you're stuck, or what the problem is with the code you've written.

1 solution

So far your code is only showing a Toast when the first list is clicked.

You also have only one list view apparently (R.id.listview -> listView).

You need to add the second list view to the layout (R.layout.activity_create_list2), add another class variable for that list view and obtain that listView2 in your onCreate method.

Then in your existing onClick handler do the stuff to add to listView2. Given what you've already put together I think you should be able to work out how to do this yourself.

BTW Just to mention that your variable naming should really be following Google best practice, specifically non-static private class vars such as listView should be mListView. This clearly wouldn't make your app spring to life but makes it easier to share code with others (the "m" in mListView reads as "my listView").
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900