1

I have created two textview and one editText when i enter the edit text value and then click on the button to get both textview and edittext value in the second value and which will show in second listview but it will show all data from first list view.

ArrayList<HashMap<String, String>> MyArrList;
String rate;
String itemn;
String quan;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuitem);
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
final ListView lisView2 = (ListView)findViewById(R.id.listView2);            

MyArrList = new ArrayList<HashMap<String, String>>();

HashMap<String, String> map;

/*** Rows 1 ***/
map = new HashMap<String, String>();
map.put("ID", "Butterscotch");
map.put("Code", "Rs 10");
MyArrList.add(map);

/*** Rows 2 ***/
map = new HashMap<String, String>();
map.put("ID", "Birthday Cake");
map.put("Code", "Rs 100");
MyArrList.add(map);

/*** Rows 3 ***/
map = new HashMap<String, String>();
map.put("ID", "Black  Crunch");
map.put("Code", "Rs 102");
MyArrList.add(map);

/*** Rows 4 ***/
map = new HashMap<String, String>();
map.put("ID", "Industrial Chocolate");
map.put("Code", "Rs 200");
MyArrList.add(map);

/*** Rows 5 ***/
map = new HashMap<String, String>();
map.put("ID", "Coffee Molasses Chip");
map.put("Code", " Rs 500");
MyArrList.add(map);    

/*** Rows 5 ***/
map = new HashMap<String, String>();
map.put("ID", "Coffee Molasses Chip");
map.put("Code", " Rs 500");
MyArrList.add(map);    

lisView1.setAdapter(new CountryAdapter(this));

// Get Item Input
lisView1.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub 

    Toast.makeText(Mmnue.this,""+ position  ,Toast.LENGTH_LONG).show(); 

    }
}); 

Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new OnClickListener() 
{
public void onClick(View v) 
{
    TextView txtCode = (TextView)findViewById(R.id.ColCode);
    TextView itemnm = (TextView)findViewById(R.id.ColID);
    EditText txtInput = (EditText)findViewById(R.id.txtInput);

     rate = txtCode.getText().toString();
     itemnam = txtInput.getText().toString();
     quan= itemnm.getText().toString();


    int count = lisView1.getAdapter().getCount();

Toast.makeText(Mmnue.this, itemnam + ", " + rate+ " , " + quan ,Toast.LENGTH_LONG).show(); lisView2.setAdapter(new CountryAdapter2(getApplicationContext()));

}
});   
} 

public class CountryAdapter extends BaseAdapter
{
private Context context;

public CountryAdapter(Context c)
{
//super( c, R.layout.activity_column, R.id.rowTextView, );
// TODO Auto-generated method stub
context = c;
} 

public CountryAdapter(OnClickListener onClickListener) {
    // TODO Auto-generated constructor stub


} 

public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
 LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_mmnue, null);

}

// ColID
TextView txtID = (TextView) convertView.findViewById(R.id.ColID);

txtID.setText(MyArrList.get(position).get("ID") +".");

// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
txtCode.setText(MyArrList.get(position).get("Code"));

return convertView;

}

}

public class CountryAdapter2 extends BaseAdapter
{
private Context context;

public CountryAdapter2(Context c)
{
//super( c, R.layout.activity_column, R.id.rowTextView, );
// TODO Auto-generated method stub
context = c;
} 

public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
 LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_mmnue, null);

}

// ColID
TextView txtID = (TextView) convertView.findViewById(R.id.ColID);
//txtID.setText(MyArrList.get(position).get("ID") +".");
txtID.setText(quan);
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
//  txtCode.setText(MyArrList.get(position).get("Code"));
txtCode.setText(rate);

return convertView;

}     
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
    return true;
} 

return super.onOptionsItemSelected(item);
} 
}
Ravi Kumawat
  • 27
  • 1
  • 7

1 Answers1

0

but it will show all data from first list view.

Because you are returning MyArrList.size(); from getCount() of CountryAdapter2. so change it to 1 because you showing only single row in second ListView as:

public int getCount() {
  // TODO Auto-generated method stub
  return 1;
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • but if i select two record then it will show only one,i have to show all selected record from first listview – Ravi Kumawat Sep 09 '14 at 06:03
  • @RaviKumawat : then use an int count and set value on btnGetItem click then return from `getCount()` of second listview.like if user selected 2 rows then set count=2; at return it from `getCount()` of second listview – ρяσѕρєя K Sep 09 '14 at 06:05
  • @RaviKumawat : see [ How to get Selected items from Multi Select List View ](http://stackoverflow.com/questions/4590856/how-to-get-selected-items-from-multi-select-list-view) example may help you in solving issue – ρяσѕρєя K Sep 09 '14 at 06:19
  • Ok but how to count get selected record from first listview – Ravi Kumawat Sep 09 '14 at 06:20
  • OK thanx ..i have one more problem i want to show selected record on toast, in my code it show only first record on Toast – Ravi Kumawat Sep 09 '14 at 07:05
  • @RaviKumawat : Use `StringBuilder` to prepare an formatted String with `rate` and `quan` then pass it as second `param` of `Toast.makeToast` method. – ρяσѕρєя K Sep 09 '14 at 07:08